gpt4 book ai didi

python - 在 setuptools 安装的脚本中使用 multiprocessing.Process

转载 作者:太空宇宙 更新时间:2023-11-04 06:01:27 25 4
gpt4 key购买 nike

我已经编写了由 setuptools 部署的 python 脚本。我在这些脚本中使用 multiprocess.Process,但如果我启动部署的脚本,它会失败并显示此错误消息:

Traceback (most recent call last):
File "C:\Python34\Scripts\process_demo.py", line 4, in <module>
__import__('pkg_resources').run_script('procdemo==1.0', 'process_demo.py')
File "C:\Python34\lib\site-packages\setuptools-5.4.1-py3.4.egg\pkg_resources.py", line 534, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "C:\Python34\lib\site-packages\setuptools-5.4.1-py3.4.egg\pkg_resources.py", line 1445, in run_script
"Can't perform this operation for unregistered loader type"
File "C:\Python34\lib\site-packages\procdemo-1.0-py3.4.egg\EGG-INFO\scripts\process_demo.py", line 19, in <module>
File "C:\Python34\lib\site-packages\procdemo-1.0-py3.4.egg\EGG-INFO\scripts\process_demo.py", line 14, in main
File "C:\Python34\lib\multiprocessing\process.py", line 105, in start
self._popen = self._Popen(self)
File "C:\Python34\lib\multiprocessing\context.py", line 212, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Python34\lib\multiprocessing\context.py", line 313, in _Popen
return Popen(process_obj)
File "C:\Python34\lib\multiprocessing\popen_spawn_win32.py", line 34, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:\Python34\lib\multiprocessing\spawn.py", line 173, in get_preparation_data
main_mod_name = getattr(main_module.__spec__, "name", None)
AttributeError: 'module' object has no attribute '__spec__'

我创建了一个简短的测试文件,其中仅包含 multiprocessing.Process 的调用,问题显然就在那里。

process_demo.py

import multiprocessing

def worker():
print("foooo")

def main(threads=4):
procs = []

for i in range(threads):
p = multiprocessing.Process(target=worker)
procs.append(p)
p.start()

if __name__ == "__main__":
multiprocessing.freeze_support()
main()

安装文件:

from ez_setup import use_setuptools
use_setuptools()

from setuptools import setup

setup(name='procdemo',
version='1.0',
description='Some Demo',
scripts=['bin/process_demo.py'],
)

如何在不破坏设置工具的情况下安全地在脚本中启动进程?

最佳答案

我不确定是否有另一种方法,但如果你用 python 脚本构建一个控制台应用程序,它就可以正常工作:

from ez_setup import use_setuptools
use_setuptools()

from setuptools import setup

setup(name='procdemo',
version='1.0',
description='Some Demo',
scripts=['bin/process_demo.py'],
entry_points = {'console_scripts': ['foobarfoo = process_demo:main']},
)

关于python - 在 setuptools 安装的脚本中使用 multiprocessing.Process,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24799146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com