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

时间:2014-07-17 09:01:37

标签: python setuptools

我编写了由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'],
      )

如何在不破坏setuptools的情况下安全地启动脚本内的进程?

1 个答案:

答案 0 :(得分:1)

我不确定是否还有其他方法,但是如果你在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']},
      )