ThreadPool没问题,Pool引发了RuntimeError

时间:2016-12-27 08:37:05

标签: python python-3.x multiprocessing

我是Python新手,我正在尝试组织某种超时,当进程挂起时。以下代码工作正常:

from multiprocessing.pool import ThreadPool
pool = ThreadPool(processes=1)
file_list = []
while not file_list:
     async_result = pool.apply_async(list_retriever,)
     try:
          file_list = async_result.get(15)
     except:
         print('We\'ve got timeout!\n')

我发现ThreadPool没有正确记录,我决定转而使用Pool。以下代码引发RuntimeError:

from multiprocessing.pool import Pool
pool = Pool(processes=1)
file_list = []
while not file_list:
     async_result = pool.apply_async(list_retriever,)
     try:
          file_list = async_result.get(15)
     except:
          print('We\'ve got timeout!\n')

    Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\multiprocessing\spawn.py", line 106, in spawn_main
    exitcode = _main(fd)
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\multiprocessing\spawn.py", line 115, in _main
    prepare(preparation_data)
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\multiprocessing\spawn.py", line 226, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\multiprocessing\spawn.py", line 278, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\runpy.py", line 254, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\Bodnya\PycharmProjects\zuf-test-branch\zuf-test\auto-deploy.py", line 69, in <module>
    pool = Pool(processes=1)
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\multiprocessing\pool.py", line 168, in __init__
    self._repopulate_pool()
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\multiprocessing\pool.py", line 233, in _repopulate_pool
    w.start()
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\multiprocessing\context.py", line 313, in _Popen
    return Popen(process_obj)
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\multiprocessing\popen_spawn_win32.py", line 34, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\multiprocessing\spawn.py", line 144, in get_preparation_data
    _check_not_importing_main()
  File "C:\Users\Bodnya\AppData\Local\Programs\Python\Python35\lib\multiprocessing\spawn.py", line 137, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

此错误消息一直显示在无限循环中。你能告诉我我做错了吗?

更新

显然我没有保护这样的主代码,以避免递归创建子进程,但为什么它在第一个例子中起作用?

1 个答案:

答案 0 :(得分:0)

这是工作解决方案:     def函数():         来自multiprocessing.pool导入ThreadPool         pool = ThreadPool(processes = 3)         file_list = []         我在范围内(3)              async_result = pool.apply_async(list_retriever,)              尝试:                   file_list = async_result.get(15)              除了:                  打印('我们有超时!\ n')                  继续     如果__name__ =='__ main__':          功能() 然而,它仍然在解释,为什么它首先起作用。