简体   繁体   English

Python 中的多处理创建单个进程

[英]Multiprocessing in Python creates single process

I have an issue with multiprocessing in python, refer to my code below:我在 python 中遇到了多处理问题,请参阅下面的代码:

processArray = []
downtimeObj = []
for x in range(0,len(clientMaster)):
    downtimeObj.append(Downtime(clientMaster[x]))
    processArray.append(multiprocessing.Process(target=downtimeObj[x].live(),))
    for j in range(len(processArray)):
        processArray[j].start()
    for z in range(len(processArray)):
        processArray[z].join()

Here I have an array of processes "processArray" and an array of objects of class Downtime.在这里,我有一个进程数组“processArray”和一个 class Downtime 对象数组。 In my client master, im trying to iterate through the number of clients I have and create an object for each client, thereby creating a process for each client.在我的客户端主机中,我试图遍历我拥有的客户端数量并为每个客户端创建一个 object,从而为每个客户端创建一个进程。

The current code creates a single process and runs the live() function chronologically.当前代码创建单个进程并按时间顺序运行 live() function。

I wish for all clients to have separate processes and the object.live() function to run simultaneously.我希望所有客户端都有单独的进程,并且 object.live() function 同时运行。

You should change indentation and first run loop which only creates all processes.您应该更改仅创建所有进程的缩进和第一个运行循环。 Next loop which runs .start() .运行.start()的下一个循环。 And finally loop which use .join()最后循环使用.join()

And Process() (similar to Thread() ) needs function's name without () so later it can use () to start in in new process.并且Process() (类似于Thread() )需要没有()的函数名称,因此稍后它可以使用()开始新进程。

target=downtimeObj[x].live

If you use with () then it runs live() in current process and send its result as argument for Process() like如果您使用 with ()那么它会在当前进程中运行live()并将其结果作为Process()的参数发送,例如

result = downtimeObj[x].live()
Process(target=result)

You could also learn to use for -loop without range(len()) - it will be more readable.您还可以学习使用for -loop 而不使用range(len()) - 它会更具可读性。

processArray = []
downtimeObj = []

# --- loop ---

for item in clientMaster:
    obj = Downtime(item)
    downtimeObj.append(obj)
    p = multiprocessing.Process(target=obj.live)
    processArray.append(p)

# --- after loop ---

for p in processArray:
    p.start()

for p in processesArray:
    p.join()

OR you should at least .join() run outside first loop或者您至少应该.join()在第一个循环之外运行

processArray = []
downtimeObj = []

# --- loop ---

for item in clientMaster:
    obj = Downtime(item)
    downtimeObj.append(obj)
    p = multiprocessing.Process(target=obj.live)
    processArray.append(p)
    p.start()

# --- after loop ---

for p in processesArray:
    p.join()

EDIT:编辑:

If you need to send arguments then use tuple ie.如果您需要发送 arguments 然后使用元组,即。 (value1, value2)

Process(target=obj.live, args=(value1, value2))

and will run live(value1, value2)并将live(value1, value2)

For single argument you also need tuple ie.对于单个参数,您还需要元组,即。 (value1,)
It needs , inside ( ) to create tuple with single element.它需要, inside ( )来创建具有单个元素的元组。

Process(target=obj.live, args=(value1,))

and will run live(value1)并将live(value1)

You can see it also in documentation Process您也可以在文档过程中看到它


EDIT:编辑:

If you want to use Pool to always runs only 5 processes and get all results如果您想使用Pool始终只运行 5 个进程并获得所有结果

from multiprocessing import Pool

# --- function for Process ---

def my_function(item, arg1, arg2):
    obj = Downtime(item)
    return obj.live(arg1, arg2)

# --- create arguments for all processes ---

arguments = []

for item in clientMaster:
    arguments.append( (item, value1, value2) )

# --- use Pool and wait for all results ---

with Pool(5) as p:
     results = p.starmap(my_function, arguments)

# --- display all results ---

print(results)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM