簡體   English   中英

使用嵌套對象的Python多處理

[英]Python multiprocessing using nested objects

我正在編寫一種優化算法,它使用幾種不同的初始條件來增加找到全局最優的機會。 我正在嘗試通過使用多處理庫並在不同進程上運行優化來使代碼運行得更快。

這是我的代碼現在基本上工作的方式:

from multiprocessing import Process, Queue
from SupportCostModel.SupportStructure import SupportStructure, SupportType

# Method the processes will execute
def optimizeAlgoritm(optimizeObject, qOut):

    optimizeObject.Optimize()
    qOut.put(optimizeObject)

# Method the main thread will execute
def getOptimumalObject(n):

    for i in range(n):

        # Create a new process with a new nested object that should be optimized
        p = Process(target = optimizeAlgoritm, args = (SupportStructure(SupportType.Monopile), qOut))
        processes.append(p)
        p.deamon = True
        p.start()

# Part the main thread is running        
if __name__ == '__main__':

    qOut = Queue()
    processes = []

    # Run the code on 6 processes
    getOptimumalObject(6)

    for i in range(len(processes)):
        processes[i].join()

    # Get the best optimized object and print the resulting value
    minimum = 1000000000000000000000000.

    while not qOut.empty():

        optimizeObject = qOut.get()

        if optimizeObject.GetTotalMass() < minimum:

            bestObject = optimizeObject
            minumum = optimizeObject.GetTotalMass()

    print(bestObject.GetTotalMass())

只要我只使用4個進程,此代碼就可以運行。 如果我運行超過4,比如示例中的6,那么兩個進程將停留在代碼的末尾,代碼將永遠不會停止運行,因為主線程仍然停留在processes[i].join() 我認為這兩個進程在qOut.put()中的qOut.put()中存在問題。 當我刪除qOut.put() ,代碼退出,給出錯誤,即bestObject不存在,正如預期的那樣。 然而,奇怪的是,如果我打印,例如, qOut.put()之后的對象最小,它將打印它,但是使用0%的CPU,進程將保持活着狀態。 這迫使主要代碼保持活力。

我對多處理非常陌生,並認為OOP和多處理並不總是能夠很好地協同工作。 我在這里使用了錯誤的方法嗎? 它有點令人沮喪,因為它幾乎可以工作,但不適用於4個以上的流程。

提前致謝!

我用它來管理我的物體!

這是我使用的代碼:

from multiprocessing import Process, Pipe
from SupportCostModel.SupportStructure import SupportStructure, SupportType
import random

# Method the processes will execute
def optimizeAlgoritm(optimizeObject, conn):

    optimizeObject.Optimize()

    # Send the optimized object
    conn.send(optimizeObject)

# Method the main thread will execute
def getOptimumalObject(n):

    connections = []

    for i in range(n):

        # Create a pipe for each of the processes that is started
        parent_conn, child_conn = Pipe()

        # Save the parent connections
        connections.append(parent_conn)

        # Create objects that needs to by optimized using different initial conditions
        if i == 0:
            structure = SupportStructure(SupportType.Monopile)
        else:
            structure = SupportStructure(SupportType.Monopile)
            structure.properties.D_mp = random.randrange(4., 10.)
            structure.properties.Dtrat_tower = random.randrange(90., 120.)
            structure.properties.Dtrat_mud = random.randrange(60., 100.)
            structure.properties.Dtrat_mp = random.randrange(60., 100.)
            structure.UpdateAll()

        # Create a new process with a new nested object that should be optimized
        p = Process(target = optimizeAlgoritm, args = (structure, child_conn))
        processes.append(p)
        p.deamon = True
        p.start()

    # Receive the optimized objects
    for i in range(n):
        optimizedObjects.append(connections[i].recv())

# Part the main thread is running        
if __name__ == '__main__':

    processes = []
    optimizedObjects = []

    # Run the code on 6 processes
    getOptimumalObject(6)

    for i in range(len(processes)):
        processes[i].join()

    # Get the best optimized object and print the resulting value
    minimum = 1000000000000000000000000.

    for i in range(len(optimizedObjects)):

        optimizeObject = optimizedObjects[i]

        if optimizeObject.GetTotalMass() < minimum:

            bestObject = optimizeObject
            minumum = optimizeObject.GetTotalMass()

    print(bestObject.GetTotalMass())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM