停止在无限循环中连接到队列的python多处理工作进程的最干净方法是什么?
我正在python中使用停止在无限循环中连接到队列的python多处理工作进程的最干净方法是什么?,python,multiprocessing,gevent,defunct,Python,Multiprocessing,Gevent,Defunct,我正在python中使用多处理.Pool和多处理.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_wo
多处理.Pool
和多处理.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()
现在,当我尝试退出时,会出现以下状态:
futex(0x7f99d9188000,futex_WAIT,0,NULL…
)那么,什么是干净地结束这样一个过程的正确方法呢?我解决了这个问题。根据,
pool
需要close()
才能join()ed
。在pool.join()之前添加pool.close()
解决了这个问题