我有这样的循环函数:
from multiprocessing import Pool
def foo_1(i):
count = 1
for something:
# blah blah blah
h=0
while len()<count:
def foo_2(j):
# blah blah blah
return i + j
h = h+1
count +=1
if __name__ == '__main__':
pool = Pool(4)
pool.imap_unordered(foo_2, range()):
pool.close()
pool.join()
语法应该是什么样子才能使其工作?因为在foo_1
里面给if __name__ == '__main __'
是行不通的,如果我把它放在最后,它就无法识别foo_2
的功能。或者,也许您需要完全重建代码语法?
我会改变代码的结构。 以下是我要做的。 只是好奇,为什么要在foo_2副foo_1上使用多处理?
from multiprocessing import Pool
# method broken out
def foo_2(j, i):
# blah blah blah
return i + j
def foo_1(i):
count = 1
for something:
# blah blah blah
h=0
while len()<count:
return_foo_2 = foo_2(j, i)
h = h+1
count +=1
if __name__ == '__main__':
pool = Pool(4)
# you will have to create a list of tasks to run
# in your multiprocessing pool
list_tasks = [(range(), range())]
pool.imap_unordered(foo_2, list_tasks):
pool.close()
pool.join()