Vik*_*kas 19 python multiprocessing defunct gevent
我正在使用multiprocessing.Pool和实现python中的生产者 - 消费者模式multiprocessing.Queue.消费者是gevent用于产生多个任务的预分叉进程.
这是一个精简版的代码:
import gevent
from Queue import Empty as QueueEmpty
from multiprocessing import Process, Queue, Pool
import signal
import time
# Task queue
queue = Queue()
def init_worker ():
# Ignore signals in worker
signal.signal( signal.SIGTERM, signal.SIG_IGN )
signal.signal( signal.SIGINT, signal.SIG_IGN )
signal.signal( signal.SIGQUIT, signal.SIG_IGN )
# One of the worker task
def worker_task1( ):
while True:
try:
m = queue.get( timeout = 2 )
# Break out if producer says quit
if m == 'QUIT':
print 'TIME TO QUIT'
break
except QueueEmpty:
pass
# Worker
def work( ):
gevent.joinall([
gevent.spawn( worker_task1 ),
])
pool = Pool( 2, init_worker )
for i in xrange( 2 ):
pool.apply_async( work )
try:
while True:
queue.put( 'Some Task' )
time.sleep( 2 )
except KeyboardInterrupt as e:
print 'STOPPING'
# Signal all workers to quit
for i in xrange( 2 ):
queue.put( 'QUIT' )
pool.join()
Run Code Online (Sandbox Code Playgroud)
现在当我尝试退出它时,我得到以下状态:
futex(0x7f99d9188000, FUTEX_WAIT, 0, NULL ....那么干净地结束这样一个过程的正确方法是什么?
Vik*_*kas 13
我解决了这个问题.根据文档multiprocessing.Pool.join(),pool需要close()ed在它之前join()ed.添加pool.close()之前pool.join()解决了问题.
| 归档时间: |
|
| 查看次数: |
9512 次 |
| 最近记录: |