python - 如何初始化目标不带参数的 Python 多处理进程?
问题描述
粗略的代码:
from multiprocessing import Process
def getPAprofilesPages():
#do stuff here
def getPBprofilesPages():
#do stuff here
P1 = Process(target = getPAprofilesPages, args = [] )
P2 = Process(target = getPBprofilesPages, args = [] )
请注意,这些函数没有参数。如上所示,我尝试将 args 设置为 None、()、(,) 和 [],并从初始化中完全忽略它。无论如何,我在尝试运行P1.start()
或P2.start()
在解释器中遇到相同的错误:
>>> Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\******\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\Users\******\AppData\Local\Programs\Python\Python37\lib\multiprocessing\spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
AttributeError: Can't get attribute 'getPAprofilesPages' on <module '__main__' (built-in)>
解决方案
以下代码在脚本中运行良好
def main():
...
all your other code goes here
...
from multiprocessing import Process
P1 = Process(target = getPAprofilesPages )
P2 = Process(target = getPBprofilesPages )
P1.start()
P2.start()
def getPAprofilesPages():
#do stuff here
pass
def getPBprofilesPages():
#do stuff here
pass
if __name__ == '__main__':
main()
但是,您说您在解释器中运行它,这就是您的问题所在,因为您不能在交互式 Python 中使用 multiprocessing 包。
我知道这不是您要寻找的答案,但它解释了您的错误。您可以在该链接中阅读有关解决方法的更多信息。
推荐阅读
- java - 如何让 ApplicationContext 识别来自 src/test/java 的 CouchBaseRepositoyr bean?
- android - 将联系人姓名输入android应用程序并保存
- vuejs2 - 使用带有 css 特定复选框的 vue 和 v-model
- html - 将 div 高度扩展到它的孩子(iframe)
- reactjs - ReactJS-只有最新的文本行出现在网页中
- php - 如何使用 Laravel 获取以前的外部链接?
- sql-server - 使用 Entity Framework Core 在 SQL Server 中存储大字符串的最佳实践
- iis-7 - IIS 管理器中的停止服务器是否与 iisReset /stop 相同?
- java - 在 AWS Fargate 上运行时如何确定当前可用区
- java - 坚持使用这个单词搜索程序