Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
停止在无限循环中连接到队列的python多处理工作进程的最干净方法是什么?_Python_Multiprocessing_Gevent_Defunct - Fatal编程技术网

停止在无限循环中连接到队列的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

我正在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_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()
    解决了这个问题