Python多进程 AttributeError: Can’t get attribute ‘worker’ on module ‘__main__’ from

  • Post author:
  • Post category:python


将创建进程池对象放在子进程函数之前会产生

AttributeError: Can't get attribute 'worker' on <module '__main__' from '/home/tian/file/pool.py'>
from multiprocessing import Pool
from time import sleep,ctime

#创建进程池对象
pool = Pool()

def worker(msg):
    sleep(2)
    print(msg)
    return msg


result = []
#向进程池添加事件
for i in range(10):
    msg = 'hello %d'%i
    r = pool.apply_async(func=worker,args=(msg,))
    result.append(r)

#关闭进程池
pool.close()
#回收进程池
pool.join()
print('=================')
for i in result:
    print(i.get())

放在子进程函数之后就不会产生这样的错误

from multiprocessing import Pool
from time import sleep,ctime

def worker(msg):
    sleep(2)
    print(msg)
    return msg
#创建进程池对象
pool = Pool()

result = []
#向进程池添加事件
for i in range(10):
    msg = 'hello %d'%i
    r = pool.apply_async(func=worker,args=(msg,))
    result.append(r)

#关闭进程池
pool.close()
#回收进程池
pool.join()
print('=================')
for i in result:
    print(i.get())



版权声明:本文为qq_37917864原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。