简体   繁体   English

Python 多处理未同时运行

[英]Python Multiprocessing not running simultaneously

Let's say I have this simple function:假设我有这个简单的 function:

def print_name(name):
    print(name)
    time.sleep(10)
    print('finished sleeping @ ', str(dt.datetime.now()))

I'm trying to use multiprocessing to run this in a loop for several names all at once, like in the below:我正在尝试使用多处理在一个循环中同时为多个名称运行它,如下所示:

from multiprocessing.pool import ThreadPool as Pool

names = ['A','B','C','D','E']
with Pool() as pool:
    for name in names:
        pool.map(print_name,[name])

However, it doesn't run simultaneously, it runs one after the other as you can see:但是,它不会同时运行,它会一个接一个地运行,如您所见:

A
finished sleeping @  2022-07-26 11:03:12.394843
B
finished sleeping @  2022-07-26 11:03:22.400343
.......

NB: I'm having to use ThreadPool instead of Pool as Pool just throws up a random pickling error: _pickle.PicklingError: Can't pickle <function print_name at 0x144ce5000>: attribute lookup print_name on __main__ failed注意:我必须使用ThreadPool而不是Pool ,因为Pool只会引发随机酸洗错误: _pickle.PicklingError: Can't pickle <function print_name at 0x144ce5000>: attribute lookup print_name on __main__ failed

I have also seen people talking about pathos.multiprocessing but that throws up this error NotImplementedError: pool objects cannot be passed between processes or pickled .我还看到人们谈论pathos.multiprocessing ,但这会引发此错误NotImplementedError: pool objects cannot be passed between processes or pickled pass between processes or pickled 。

ThreadPool is the only way I can get any form of multiprocessing to at least not throw in an error message. ThreadPool是我可以获得任何形式的多处理至少不会抛出错误消息的唯一方法。 So possibly that's my problem?所以这可能是我的问题?

Ultimately I'm hoping to be able to use multiprocessing as I have a big function that takes about 15 mins to run, and I need to run it over a list of about 100 items so multiprocessing would be really handy.最终我希望能够使用多处理,因为我有一个大的 function 需要大约 15 分钟才能运行,我需要在大约 100 个项目的列表上运行它,所以多处理会非常方便。 But I can't even get this simple example to work at the minute and I'm a bit stuck, so any help would be really appreciated.但我现在什至无法让这个简单的例子工作,而且我有点卡住了,所以任何帮助都将不胜感激。

You may want to consider using ProcessPoolExecutor from concurrent.futures but in the meantime, this may help:您可能需要考虑使用 concurrent.futures 中的 ProcessPoolExecutor 但与此同时,这可能会有所帮助:

import datetime as dt
import time
from multiprocessing import Pool

def print_name(name):
    print(name)
    time.sleep(5)
    print('finished sleeping @ ', dt.datetime.now())

names = ['A','B','C','D','E']

def main():
    with Pool() as pool:
        pool.map(print_name, names)

if __name__ == '__main__':
    main()

Output: Output:

A
B
C
D
E
finished sleeping @  2022-07-26 11:29:31.277722
finished sleeping @  2022-07-26 11:29:31.277749
finished sleeping @  2022-07-26 11:29:31.285708
finished sleeping @  2022-07-26 11:29:31.292636
finished sleeping @  2022-07-26 11:29:31.295505

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

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