如何在以下代码中修复管道破裂的错误?

时间:2019-08-23 13:05:29

标签: python multiprocessing broken-pipe

当我运行代码的以下部分时,它会产生管道破裂错误。如何解决此问题?该错误通常涉及代码行,该行显示为agent [0] .start()。谢谢。

def main():

   np.random.seed(RANDOM_SEED)
   assert len(PACKET_SIZE) == A_DIM

   # create result directory
   if not os.path.exists(SUMMARY_DIR):
       os.makedirs(SUMMARY_DIR)

   # inter-process communication queues
   net_params_queues = []
   exp_queues = []
   for i in range(NUM_AGENTS):
       net_params_queues.append(mp.Queue(1))
       exp_queues.append(mp.Queue(1))


   # create a coordinator and multiple agent processes
   # (note: threading is not desirable due to python GIL)
   coordinator = mp.Process(target=central_agent,
                            args=(net_params_queues, exp_queues))
   coordinator.start()
   all_cooked_time, all_cooked_bw, _ = load_trace.load_trace(TRAIN_TRACES)
   agents = []
   print(NUM_AGENTS)
   for i in range(NUM_AGENTS):
       agents.append(mp.Process(target=agent,
                                args=(i, all_cooked_time, all_cooked_bw,
                                      net_params_queues[i],
                                      exp_queues[i])))
   #print(agents)
   #for i in range(NUM_AGENTS):
   agents[0].start()

   # wait unit training is done
   coordinator.join()


if __name__ == '__main__':
   main()

完整的追溯如下所示。如果有必要,我可以添加程序的完整代码。我已经从某个地方复制了它,并且试图理解它。


BrokenPipeError                           Traceback (most recent call last)
<ipython-input-24-152dc3806edb> in <module>
    332 
    333 if __name__ == '__main__':
--> 334     main()

<ipython-input-24-152dc3806edb> in main()
    325     #print(agents)
    326     #for i in range(NUM_AGENTS):
--> 327     agents[0].start()
    328 
    329     # wait unit training is done

C:\ProgramData\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

C:\ProgramData\Anaconda3\lib\multiprocessing\context.py in _Popen(process_obj)
    221     @staticmethod
    222     def _Popen(process_obj):
--> 223         return _default_context.get_context().Process._Popen(process_obj)
    224 
    225 class DefaultContext(BaseContext):

C:\ProgramData\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):

C:\ProgramData\Anaconda3\lib\multiprocessing\popen_spawn_win32.py in __init__(self, process_obj)
     63             try:
     64                 reduction.dump(prep_data, to_child)
---> 65                 reduction.dump(process_obj, to_child)
     66             finally:
     67                 set_spawning_popen(None)

C:\ProgramData\Anaconda3\lib\multiprocessing\reduction.py in dump(obj, file, protocol)
     58 def dump(obj, file, protocol=None):
     59     '''Replacement for pickle.dump() using ForkingPickler.'''
---> 60     ForkingPickler(file, protocol).dump(obj)
     61 
     62 #

BrokenPipeError: [Errno 32] Broken pipe

1 个答案:

答案 0 :(得分:1)

从回溯中可以明显看出,您正在使用ms-windows上的IPython中的multiprocessing

不幸的是,这不能很好地工作。 AFAICT,这与IPython如何在笔记本中运行代码以及multiprocessing在ms-windows上的工作方式有关。

尝试将代码保存在文件中,然后从命令行而不是在IPython中运行该脚本。