Python Issue with creating children process of children process in python

2024-04-26 22:41:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python中有两个文件proc1.py和proc2.py,这两个文件都是单生产者、多消费者场景的实现。proc1在其main中创建子进程。在每个子进程中,它调用proc2。proc2在其代码中也创建子进程。从命令行执行proc2.py可以正常工作。但是当从proc1调用时,我得到了错误

Traceback (most recent call last):
  File "./proc2.py", line 20, in proc2WorkerFunc
    elem = q.get(block = False)
  File "/usr/lib64/python2.6/multiprocessing/queues.py", line 103, in get
    if not self._poll(block and (deadline-time.time()) or 0.0):
IOError: [Errno 9] Bad file descriptor

我试过在网上搜索,但到目前为止没有任何线索。你知道我做错了什么吗? Proc1.py是

^{pr2}$

程序2.py:

def proc2WorkerFunc(q):
  while True:
    try:
      print q.qsize()
      elem = q.get(block = False)
      print elem
      time.sleep(0.5)
    except Queue.Empty:
      print "Queue is empty"
      return
    except:
      print "Exception happened"
      tb = traceback.format_exc()
      print tb
      return




if __name__ == '__main__':
  proc2Q = multiprocessing.Queue()
  for i  in range(100):
    proc2Q.put('World')

  num_workers = 10
  workers = []
  for i in range(num_workers):
    qWorker = multiprocessing.Process(target=proc2WorkerFunc, args=(proc2Q,))
    qWorker.start()
    workers.append(qWorker)

  for worker in workers:
    worker.join()

Tags: inpygettimequeue进程blockmultiprocessing