Python3 in _check_not_importing_main is not going to be frozen to produce an executable.''')
阿新 • • 發佈:2019-01-01
知道自己知道什麼,也知道自己不知道什麼,這就是真正的知識。--------梭羅
在進行python3多程序multiprocessing學習過程中,在Windows環境下測試分散式程式碼過程中,利用fork()生成child processes的程式碼須放在main模組中,否則將會報錯:
程式碼如下:
報錯資訊:import queue import random from multiprocessing.managers import BaseManager task_queue = queue.Queue() result_queue = queue.Queue() def return_task_queue(): global task_queue return task_queue def return_result_queue(): global result_queue return result_queue class QueueManager(BaseManager): pass QueueManager.register('get_task_queue', callable=return_task_queue) QueueManager.register('get_result_queue', callable=return_result_queue) manager = QueueManager(address=('127.0.0.1', 5000), authkey=b'abc') manager.start() task = manager.get_task_queue() result = manager.get_result_queue() for i in range(10): n = random.randint(0, 10000) print('Put task %d' % n) task.put(n) print('Try get results..') for i in range(10): r = result.get(timeout=10) print('Result:%s' % r) manager.shutdown() print('master exit.')
所以需要調整程式碼,把相應呼叫程式碼放在main模組中:"E:\python\python project\myfirst\venv\Scripts\python.exe" "E:/python/python project/myfirst/vari/distibuted_master.py" Traceback (most recent call last): File "<string>", line 1, in <module> File "D:\program files\Python3.6\Lib\multiprocessing\spawn.py", line 105, in spawn_main exitcode = _main(fd) File "D:\program files\Python3.6\Lib\multiprocessing\spawn.py", line 114, in _main prepare(preparation_data) File "D:\program files\Python3.6\Lib\multiprocessing\spawn.py", line 225, in prepare _fixup_main_from_path(data['init_main_from_path']) File "D:\program files\Python3.6\Lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path run_name="__mp_main__") File "D:\program files\Python3.6\Lib\runpy.py", line 263, in run_path pkg_name=pkg_name, script_name=fname) File "D:\program files\Python3.6\Lib\runpy.py", line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name) File "D:\program files\Python3.6\Lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "E:\python\python project\myfirst\vari\distibuted_master.py", line 24, in <module> manager.start() File "D:\program files\Python3.6\Lib\multiprocessing\managers.py", line 513, in start self._process.start() File "D:\program files\Python3.6\Lib\multiprocessing\process.py", line 105, in start self._popen = self._Popen(self) File "D:\program files\Python3.6\Lib\multiprocessing\context.py", line 322, in _Popen return Popen(process_obj) File "D:\program files\Python3.6\Lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__ prep_data = spawn.get_preparation_data(process_obj._name) File "D:\program files\Python3.6\Lib\multiprocessing\spawn.py", line 143, in get_preparation_data _check_not_importing_main() File "D:\program files\Python3.6\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. Process finished with exit code 1
結果如下:import queue import random from multiprocessing.managers import BaseManager task_queue = queue.Queue() result_queue = queue.Queue() def return_task_queue(): global task_queue return task_queue def return_result_queue(): global result_queue return result_queue class QueueManager(BaseManager): pass if __name__=='__main__': QueueManager.register('get_task_queue', callable=return_task_queue) QueueManager.register('get_result_queue', callable=return_result_queue) manager = QueueManager(address=('127.0.0.1', 5000), authkey=b'abc') manager.start() task = manager.get_task_queue() result = manager.get_result_queue() for i in range(10): n = random.randint(0, 10000) print('Put task %d' % n) task.put(n) print('Try get results..') for i in range(10): r = result.get(timeout=10) print('Result:%s' % r) manager.shutdown() print('master exit.')
"E:\python\python project\myfirst\venv\Scripts\python.exe" "E:/python/python project/myfirst/vari/distibuted_master.py"
Put task 5292
Put task 5986
Put task 3500
Put task 2385
Put task 8343
Put task 6482
Put task 4331
Put task 6104
Put task 9341
Put task 5156
Try get results..
Process finished with exit code 1
喜歡的朋友可以掃描以下二維碼進行關注,公眾號將每天更新文章: