假设我有一个简单的函数:
def print_name(name):
print(name)
time.sleep(10)
print('finished sleeping @ ', str(dt.datetime.now()))
我正试图使用多处理来同时对几个名称在循环中运行此操作,如下所示:
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])
然而,它并不是同时运行的,它一个接一个地运行,正如你所看到的:
A
finished sleeping @ 2022-07-26 11:03:12.394843
B
finished sleeping @ 2022-07-26 11:03:22.400343
.......
注意:我不得不使用ThreadPool
而不是Pool
,因为Pool
只是抛出一个随机酸洗错误:_pickle.PicklingError: Can't pickle <function print_name at 0x144ce5000>: attribute lookup print_name on __main__ failed
我也看到有人在谈论pathos.multiprocessing
,但这会引发这个错误NotImplementedError: pool objects cannot be passed between processes or pickled
。
ThreadPool
是我获得任何形式的多处理的唯一方法,至少不会抛出错误消息。所以这可能是我的问题?
最终,我希望能够使用多处理,因为我有一个大约需要15分钟运行的大函数,并且我需要在大约100个项目的列表上运行它,所以多处理将非常方便。但我现在甚至无法让这个简单的例子发挥作用,我有点陷入困境,所以任何帮助都将不胜感激。
您可能需要考虑使用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()
输出:
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