'fun test'
from multiprocessing.pool import Pool
import time
if __name__ == '__main__':
print('multiprocessing test ==============')
def ppfun(id):
print('proc(%d) start' %id)
time.sleep(2)
print('proc(%d) end' %id)
return str(id) + 'done'
p = Pool(processes = 3)
results = []
for i in range(4):
results.append(p.apply_async(ppfun, args = (i,)))
p.close()
p.join()
for res in results:
print(type(res))
print(res.get())
print('process(es) all done')
print('FIN')
以上代码,apply_async()的执行函数参数ppfun在
if __name__ == '__main__':
中定义,执行时会报异常:
AtrributeError: Can't get atrribute 'ppfun' on <module '__mp_main__' from 'E:\\...\\....\\funtest.py'>
穆罕穆德易卜拉欣
以上代码,apply_async()的执行函数参数ppfun在
中定义,执行时会报异常:
ppfun如果定义在
外面就可以正常执行。
问下这是为啥?