使用嵌套对象的Python多处理

2024-04-25 05:03:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我在写一个优化算法,它使用几个不同的初始条件来增加找到全局最优的机会。我试图通过使用多处理库并在不同的进程上运行优化,使代码运行得更快。在

我的代码现在基本上是这样工作的:

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()时,代码退出,并给出bestObject不存在的错误,如预期的那样。但是,奇怪的是,如果我打印,例如,qOut.put()之后的最小对象将打印它,但是进程将使用我的0%的CPU保持活动状态。这也迫使主代码保持活动状态。在

我对多处理还是个新手,我读到OOP和multiprocessing并不总是能很好地协同工作。我是不是用错了方法?这有点令人沮丧,因为它几乎可以工作,但不工作超过4个进程。在

提前谢谢!在


Tags: the代码fromimport算法put进程main
1条回答
网友
1楼 · 发布于 2024-04-25 05:03:57

我让它用管道来传送我的东西!在

这是我使用的代码:

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())

相关问题 更多 >