【问题标题】:Concurrent future fails on WindowsWindows 上的并发未来失败
【发布时间】:2019-12-23 10:45:01
【问题描述】:

我最近从我的 Mac 移到了一台 Windows 机器上,我正在尝试导入我在 Mac 上工作时创建的所有脚本。特别是有一个我真的需要修复的不工作。

此代码的问题在于它使用 concurrent.futures 来生成许多子进程,而在 mac 上它工作正常(刚刚在另一台计算机上测试过),在 windows 上相同的代码给了我以下错误。

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Lavoro\Dottorato\3_klip_tiles.py", line 188, in <module>
    mk_postagestamps(MainID_list,len(MainID_list),counts_df,header_df,path2dir,list_of_filters,fk_pos,workers,KLIPmodes=KLIPmodes) #build the cube dataframe form the selected UniqueID stars
  File "D:\Lavoro\Dottorato\3_klip_tiles.py", line 45, in mk_postagestamps
    for variable in executor.map(task,MainID_list,repeat(counts_df),repeat(header_df),repeat(KLIPmodes),repeat(fk_pos),repeat(filters),repeat(path2dir),chunksize=chunksize): #the task routine is where everything is done!
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 645, in map
    timeout=timeout)
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\_base.py", line 575, in map
    fs = [self.submit(fn, *args) for args in zip(*iterables)]
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\_base.py", line 575, in <listcomp>
    fs = [self.submit(fn, *args) for args in zip(*iterables)]
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 615, in submit
    self._start_queue_management_thread()
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 569, in _start_queue_management_thread
    self._adjust_process_count()
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 593, in _adjust_process_count
    p.start()
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 46, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 136, 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.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Lavoro\Dottorato\3_klip_tiles.py", line 188, in <module>
    mk_postagestamps(MainID_list,len(MainID_list),counts_df,header_df,path2dir,list_of_filters,fk_pos,workers,KLIPmodes=KLIPmodes) #build the cube dataframe form the selected UniqueID stars
  File "D:\Lavoro\Dottorato\3_klip_tiles.py", line 45, in mk_postagestamps
    for variable in executor.map(task,MainID_list,repeat(counts_df),repeat(header_df),repeat(KLIPmodes),repeat(fk_pos),repeat(filters),repeat(path2dir),chunksize=chunksize): #the task routine is where everything is done!
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 645, in map
    timeout=timeout)
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\_base.py", line 575, in map
    fs = [self.submit(fn, *args) for args in zip(*iterables)]
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\_base.py", line 575, in <listcomp>
    fs = [self.submit(fn, *args) for args in zip(*iterables)]
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 615, in submit
    self._start_queue_management_thread()
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 569, in _start_queue_management_thread
    self._adjust_process_count()
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 593, in _adjust_process_count
    p.start()
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\process.py", line 112, in start
Traceback (most recent call last):
    self._popen = self._Popen(self)
  File "<string>", line 1, in <module>
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 105, in spawn_main
    return Popen(process_obj)
    exitcode = _main(fd)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 46, in __init__
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 114, in _main
    prep_data = spawn.get_preparation_data(process_obj._name)
    prepare(preparation_data)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 225, in prepare
    _check_not_importing_main()
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    is not going to be frozen to produce an executable.''')
    run_name="__mp_main__")
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.  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 263, in run_path

    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Lavoro\Dottorato\3_klip_tiles.py", line 188, in <module>
    mk_postagestamps(MainID_list,len(MainID_list),counts_df,header_df,path2dir,list_of_filters,fk_pos,workers,KLIPmodes=KLIPmodes) #build the cube dataframe form the selected UniqueID stars
Traceback (most recent call last):
  File "D:\Lavoro\Dottorato\3_klip_tiles.py", line 45, in mk_postagestamps
  File "<string>", line 1, in <module>
    for variable in executor.map(task,MainID_list,repeat(counts_df),repeat(header_df),repeat(KLIPmodes),repeat(fk_pos),repeat(filters),repeat(path2dir),chunksize=chunksize): #the task routine is where everything is done!
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 645, in map
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 105, in spawn_main
    timeout=timeout)
    exitcode = _main(fd)
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\_base.py", line 575, in map
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 114, in _main
    fs = [self.submit(fn, *args) for args in zip(*iterables)]
    prepare(preparation_data)
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\_base.py", line 575, in <listcomp>
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 225, in prepare
    fs = [self.submit(fn, *args) for args in zip(*iterables)]
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 615, in submit
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    self._start_queue_management_thread()
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 569, in _start_queue_management_thread
    run_name="__mp_main__")
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 263, in run_path
    self._adjust_process_count()
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 593, in _adjust_process_count
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 85, in _run_code
    p.start()
    exec(code, run_globals)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\process.py", line 112, in start
  File "D:\Lavoro\Dottorato\3_klip_tiles.py", line 188, in <module>
    self._popen = self._Popen(self)
    mk_postagestamps(MainID_list,len(MainID_list),counts_df,header_df,path2dir,list_of_filters,fk_pos,workers,KLIPmodes=KLIPmodes) #build the cube dataframe form the selected UniqueID stars
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
  File "D:\Lavoro\Dottorato\3_klip_tiles.py", line 45, in mk_postagestamps
    return Popen(process_obj)
    for variable in executor.map(task,MainID_list,repeat(counts_df),repeat(header_df),repeat(KLIPmodes),repeat(fk_pos),repeat(filters),repeat(path2dir),chunksize=chunksize): #the task routine is where everything is done!
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 46, in __init__
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 645, in map
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    timeout=timeout)
    _check_not_importing_main()
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\_base.py", line 575, in map
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
    fs = [self.submit(fn, *args) for args in zip(*iterables)]
    is not going to be frozen to produce an executable.''')
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\_base.py", line 575, in <listcomp>
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.
    fs = [self.submit(fn, *args) for args in zip(*iterables)]
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 615, in submit
    self._start_queue_management_thread()
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 569, in _start_queue_management_thread
    self._adjust_process_count()
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 593, in _adjust_process_count
    p.start()
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 46, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 136, 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.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\stram\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Lavoro\Dottorato\3_klip_tiles.py", line 188, in <module>
    mk_postagestamps(MainID_list,len(MainID_list),counts_df,header_df,path2dir,list_of_filters,fk_pos,workers,KLIPmodes=KLIPmodes) #build the cube dataframe form the selected UniqueID stars
  File "D:\Lavoro\Dottorato\3_klip_tiles.py", line 45, in mk_postagestamps
    for variable in executor.map(task,MainID_list,repeat(counts_df),repeat(header_df),repeat(KLIPmodes),repeat(fk_pos),repeat(filters),repeat(path2dir),chunksize=chunksize): #the task routine is where everything is done!
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 645, in map
    timeout=timeout)
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\_base.py", line 575, in map
    fs = [self.submit(fn, *args) for args in zip(*iterables)]
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\_base.py", line 575, in <listcomp>
    fs = [self.submit(fn, *args) for args in zip(*iterables)]
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 615, in submit
    self._start_queue_management_thread()
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 569, in _start_queue_management_thread
    self._adjust_process_count()
  File "C:\Users\stram\Anaconda3\lib\concurrent\futures\process.py", line 593, in _adjust_process_count
    p.start()
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 46, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "C:\Users\stram\Anaconda3\lib\multiprocessing\spawn.py", line 136, 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.

编辑:

我在输出中应用了建议,添加了:

if __name__ == '__main__':
    freeze_support()

在代码的开头,但我现在收到一个新错误。 __spec__ 说缺少的属性是什么? 我正在运行 Anaconda Powershell promt 中的代码

AttributeError                            
Traceback (most recent call last)
D:\Lavoro\Dottorato\3_klip_tiles.py in <module>
    199 if __name__ == '__main__':
    200     freeze_support()
--> 201     main()

D:\Lavoro\Dottorato\3_klip_tiles.py in main()
    188     if MainID_sel!=None:MainID_list=[int(i) for i in MainID_sel.split(',')]
    189
--> 190     mk_postagestamps(MainID_list,len(MainID_list),counts_df,header_df,path2dir,list_of_filters,fk_pos,workers,KLIPmodes=KLIPmodes,showplot=showplot) #build the cube dataframe form the selected UniqueID stars
    191
    192     header_df.to_hdf(file_path,'header',mode='w',table=True)

D:\Lavoro\Dottorato\3_klip_tiles.py in mk_postagestamps(MainID_list, ntarget, counts_df, header_df, path2dir, filters, fk_pos, workers, showplot, use_std, KLIPmodes)
     44         chunksize = ntarget // num_of_chunks
     45         if chunksize <=0: chunksize=1
---> 46         for variable in executor.map(task,MainID_list,repeat(counts_df),repeat(header_df),repeat(KLIPmodes),repeat(fk_pos),repeat(filters),repeat(path2dir),repeat(showplot),chunksize=chunksize): #the task routine is where everything is done!
     47             continue
     48     ##########################################################

~\Anaconda3\lib\concurrent\futures\process.py in map(self, fn, timeout, chunksize, *iterables)
    643         results = super().map(partial(_process_chunk, fn),
    644                               _get_chunks(*iterables, chunksize=chunksize),
--> 645                               timeout=timeout)
    646         return _chain_from_iterable_of_lists(results)
    647

~\Anaconda3\lib\concurrent\futures\_base.py in map(self, fn, timeout, chunksize, *iterables)
    573             end_time = timeout + time.monotonic()
    574
--> 575         fs = [self.submit(fn, *args) for args in zip(*iterables)]
    576
    577         # Yield must be hidden in closure so that the futures are submitted

~\Anaconda3\lib\concurrent\futures\_base.py in <listcomp>(.0)
    573             end_time = timeout + time.monotonic()
    574
--> 575         fs = [self.submit(fn, *args) for args in zip(*iterables)]
    576
    577         # Yield must be hidden in closure so that the futures are submitted

~\Anaconda3\lib\concurrent\futures\process.py in submit(self, fn, *args, **kwargs)
    613             self._queue_management_thread_wakeup.wakeup()
    614
--> 615             self._start_queue_management_thread()
    616             return f
    617     submit.__doc__ = _base.Executor.submit.__doc__

~\Anaconda3\lib\concurrent\futures\process.py in _start_queue_management_thread(self)
    567                 thread_wakeup.wakeup()
    568             # Start the processes so that their sentinels are known.
--> 569             self._adjust_process_count()
    570             self._queue_management_thread = threading.Thread(
    571                 target=_queue_management_worker,

~\Anaconda3\lib\concurrent\futures\process.py in _adjust_process_count(self)
    591                       self._initializer,
    592                       self._initargs))
--> 593             p.start()
    594             self._processes[p.pid] = p
    595

~\Anaconda3\lib\multiprocessing\process.py in start(self)
    110                'daemonic processes are not allowed to have children'
    111         _cleanup()
--> 112         self._popen = self._Popen(self)
    113         self._sentinel = self._popen.sentinel
    114         # Avoid a refcycle if the target function holds an indirect

~\Anaconda3\lib\multiprocessing\context.py in _Popen(process_obj)
    320         def _Popen(process_obj):
    321             from .popen_spawn_win32 import Popen
--> 322             return Popen(process_obj)
    323
    324     class SpawnContext(BaseContext):

~\Anaconda3\lib\multiprocessing\popen_spawn_win32.py in __init__(self, process_obj)
     44
     45     def __init__(self, process_obj):
---> 46         prep_data = spawn.get_preparation_data(process_obj._name)
     47
     48         # read end of pipe will be "stolen" by the child process

~\Anaconda3\lib\multiprocessing\spawn.py in get_preparation_data(name)
    170     # or through direct execution (or to leave it alone entirely)
    171     main_module = sys.modules['__main__']
--> 172     main_mod_name = getattr(main_module.__spec__, "name", None)
    173     if main_mod_name is not None:
    174         d['init_main_from_name'] = main_mod_name

AttributeError: module '__main__' has no attribute '__spec__'

编辑 2:

所以最后我想我解决了。我发现了这个:Python Multiprocessing error: AttributeError: module '__main__' has no attribute '__spec__'

并按照那里的建议,我将例程修改如下

if __name__ == '__main__':
    __spec__ = None
    freeze_support()
    main()

这现在有效(我仍然不知道为什么它之前失败了,所以如果有人想解释我会非常感激)。

【问题讨论】:

  • 您是否阅读并应用了错误消息中的建议?
  • 是的,我在发布后意识到那里有一个建议,但它仍然不起作用,让我编辑问题

标签: python windows concurrent.futures


【解决方案1】:

多处理在 ms-windows 上的工作方式不同,因为该操作系统缺少 UNIX 和 macOS 上使用的fork 系统调用。

fork 将子进程创建为父进程的完美副本。两个进程中的所有代码和数据都是相同的。唯一的区别是fork 调用的返回值。 (即让新进程知道它是一个副本。) 因此子进程可以访问(副本)父进程中的所有数据。

在 ms-windows 上,多处理尝试通过启动一个新的 python 解释器来“伪造”fork,并让它导入你的主模块。 这意味着(除其他外)您的主模块必须是可导入的,没有副作用,例如启动另一个进程。因此if __name__ == '__main__' 的原因。这也意味着您的工作进程可能有权也可能无法访问在父进程中创建的数据,具体取决于创建它的位置。它可以访问之前 __main__ 创建的任何内容。但它无法访问在主块内部创建的任何内容。

【讨论】:

  • 好的,所以我设法使它与 if name == 'main' 一起工作,但我仍然有问题。从 Spyder 运行时,concurrent.futures.ProcessPoolExecutor 内部没有打印。如果我从 Anaconda Prompt shell 运行它,它工作正常,它会打印内部发生的一切,但从 Spyder 却没有。同样的程序,同样的选择,同样的一切。知道为什么吗?
  • 由于我从未使用过 Spyder,所以在这里我无法提供太多帮助。但是here,它说的是内部控制台:请不要使用它来运行您的代码
猜你喜欢
  • 2015-10-28
  • 2013-11-05
  • 2015-11-14
  • 2017-01-05
  • 2015-09-19
  • 2018-09-08
  • 2021-10-31
  • 2011-11-30
  • 1970-01-01
相关资源
最近更新 更多