多进程---错误解决AttributeError: Can't get attribute 'long_time_task' on <module '__main__' (built-in)>
来源:互联网 发布:南斯拉夫知乎 编辑:程序博客网 时间:2024/06/05 17:57
StackOverflow解决办法
这个问题似乎是多处理器的一个设计特征。请参阅https://bugs.python.org/issue25053。由于某些原因Pool不能始终使用未在导入的模块中定义的对象。所以你必须将函数写入不同的文件并导入模块。
解决办法
一个文件产生错误,把函数定义放在另一个py文件中再引入
one.py
from multiprocessing import Poolimport long_time_taskimport osif __name__=='__main__': print('Parent process %s.' % os.getpid()) p = Pool(4) for i in range(5): p.apply_async(long_time_task, args=(i,)) print('Waiting for all subprocesses done...') p.close() p.join() print('All subprocesses done.')
long_time_task.py
import os, time, randomdef long_time_task(name): print('Run task %s (%s)...' % (name, os.getpid())) start = time.time() time.sleep(random.random() * 3) end = time.time() print('Task %s runs %0.2f seconds.' % (name, (end - start)))
阅读全文
0 0
- 多进程---错误解决AttributeError: Can't get attribute 'long_time_task' on <module '__main__' (built-in)>
- Python can't find '__main__' module in ''
- Python can't find '__main__' module in ''
- (已解决)sublime text 写程序build后提示can't find '__main__' module in ' '
- AttributeError: module 'requests' has no attribute 'get'”,python中”requests模块“get”属性缺失“的错误该如何解决?
- ipython下运行代码出现<module '__main__' (built-in)> is a built-in class错误
- module rewrite_module is built-in and can't be loaded
- python2,socket多进程的错误pickle.PicklingError: Can't pickle <built-in method recvfrom_into of _socket.soc
- goagent AttributeError: can't set attribute
- goagent AttributeError: can't set attribute
- goagent AttributeError: can't set attribute
- 如何解决GoAgent上传应用时遇到AttributeError: can't set attribute的问题
- sublime text2 python error message /usr/bin/python: can't find '__main__' module in ''
- sublime text 写程序build后提示can't find '__main__' module in ' '
- PyQt5系列(三)Python3.x AttributeError: module 'string' has no attribute 'maketrans'错误解决
- apache添加模块时报错:module status_module is built-in and can't be loaded
- 解决goagent上传时can’t set attribute错误
- [python错误]builtins.AttributeError: 'module' object has no attribute 'request'
- loadrunne如何只测某个方法,或只压测录制的脚本部分代码的运行用时
- 教育中部分产品设计原稿(转发需标明)
- SSM框架下使用PageHelper分页
- 网站文章为何需要关键词做锚文本
- 深入理解equal方法和hascode方法
- 多进程---错误解决AttributeError: Can't get attribute 'long_time_task' on <module '__main__' (built-in)>
- 解决mySQL占用内存超大问题
- 服务治理 Spring Cloud Eureka
- Spring MVC快速入门教程
- Swap Nodes in Pairs
- Mybatis使用介绍
- flex布局基本属性用法示例以及常见布局
- 字符串的模式匹配中的算法
- GBDT原理理解