我可以以某种方式与子进程共享异步队列吗?

时间:2014-07-10 22:03:10

标签: python queue multiprocessing shared-memory python-asyncio

我想使用队列将数据从父级传递到通过multiprocessing.Process启动的子进程。但是,由于父进程使用Python的新asyncio库,因此队列方法必须是非阻塞的。据我所知,asyncio.Queue用于任务间通信,不能用于进程间通信。另外,我知道multiprocessing.Queue具有put_nowait()get_nowait()方法,但实际上我需要协程仍然会阻止当前任务(但不是整个过程)。有没有办法创建包装put_nowait() / get_nowait()的协同程序?另一方面,multiprocessing.Queue内部使用的线程是否与在同一进程中运行的事件循环完全兼容?

如果没有,我还有其他选择吗?我知道我可以通过使用异步套接字自己实现这样的队列,但我希望我能避免这样......

修改 我还考虑使用管道而不是套接字,但似乎asynciomultiprocessing.Pipe()不兼容。更确切地说,Pipe()返回Connection个对象的元组,这些对象不是类文件对象。但是,asyncio.BaseEventLoop方法add_reader()/add_writer()方法和connect_read_pipe()/connect_write_pipe()都需要类似文件的对象,因此无法异步读取/写入此类Connection 。相比之下,subprocess包用作管道的常见文件类对象完全没有问题can easily be used in combination with asyncio

更新 我决定进一步探索管道方法:我通过fileno()检索文件描述符并将其传递给{{1},将Connection返回的multiprocessing.Pipe()对象转换为类文件对象}}。最后,我将生成的类似文件的对象传递给事件循环os.fdopen() / connect_read_pipe()。 (如果有人对确切的代码感兴趣,则相关问题上会有一些mailing list discussion。)但是,connect_write_pipe()该流给了我一个read()而我没有设法解决这个问题。同时考虑missing support for Windows,我不会再继续这样做了。

3 个答案:

答案 0 :(得分:11)

以下是multiprocessing.Queue对象的实现,可以与asyncio一起使用。它提供了整个multiprocessing.Queue接口,添加了coro_getcoro_put方法,这些方法可以asyncio.coroutine用于异步接收/放入队列。实现细节与我的另一个答案的第二个示例基本相同:ThreadPoolExecutor用于使get / put异步,multiprocessing.managers.SyncManager.Queue用于在进程之间共享队列。唯一的附加技巧是实现__getstate__以保持对象可选,尽管使用不可选择的ThreadPoolExecutor作为实例变量。

from multiprocessing import Manager, cpu_count
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor

def AsyncProcessQueue(maxsize=0):
    m = Manager()
    q = m.Queue(maxsize=maxsize)
    return _ProcQueue(q)   

class _ProcQueue(object):
    def __init__(self, q):
        self._queue = q
        self._real_executor = None
        self._cancelled_join = False

    @property
    def _executor(self):
        if not self._real_executor:
            self._real_executor = ThreadPoolExecutor(max_workers=cpu_count())
        return self._real_executor

    def __getstate__(self):
        self_dict = self.__dict__
        self_dict['_real_executor'] = None
        return self_dict

    def __getattr__(self, name):
        if name in ['qsize', 'empty', 'full', 'put', 'put_nowait',
                    'get', 'get_nowait', 'close']:
            return getattr(self._queue, name)
        else:
            raise AttributeError("'%s' object has no attribute '%s'" % 
                                    (self.__class__.__name__, name))

    @asyncio.coroutine
    def coro_put(self, item):
        loop = asyncio.get_event_loop()
        return (yield from loop.run_in_executor(self._executor, self.put, item))

    @asyncio.coroutine    
    def coro_get(self):
        loop = asyncio.get_event_loop()
        return (yield from loop.run_in_executor(self._executor, self.get))

    def cancel_join_thread(self):
        self._cancelled_join = True
        self._queue.cancel_join_thread()

    def join_thread(self):
        self._queue.join_thread()
        if self._real_executor and not self._cancelled_join:
            self._real_executor.shutdown()

@asyncio.coroutine
def _do_coro_proc_work(q, stuff, stuff2):
    ok = stuff + stuff2
    print("Passing %s to parent" % ok)
    yield from q.coro_put(ok)  # Non-blocking
    item = q.get() # Can be used with the normal blocking API, too
    print("got %s back from parent" % item)

def do_coro_proc_work(q, stuff, stuff2):
    loop = asyncio.get_event_loop()
    loop.run_until_complete(_do_coro_proc_work(q, stuff, stuff2))

@asyncio.coroutine
def do_work(q):
    loop.run_in_executor(ProcessPoolExecutor(max_workers=1),
                         do_coro_proc_work, q, 1, 2)
    item = yield from q.coro_get()
    print("Got %s from worker" % item)
    item = item + 25
    q.put(item)

if __name__  == "__main__":
    q = AsyncProcessQueue()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(do_work(q))

输出:

Passing 3 to parent
Got 3 from worker
got 28 back from parent

如您所见,您可以从父进程或子进程同步和异步使用AsyncProcessQueue。它不需要任何全局状态,并且通过将大部分复杂性封装在一个类中,使用起来比我原来的答案更优雅。

你可能会直接使用套接字获得更好的性能,但是以跨平台的方式工作似乎相当棘手。这样做的好处是可以在多个工作人员中使用,不会要求你自己腌制/捣蛋等。

答案 1 :(得分:4)

遗憾的是,multiprocessing库并不特别适合与asyncio一起使用。但是,根据您计划使用multiprocessing / multprocessing.Queue的方式,您可以使用concurrent.futures.ProcessPoolExecutor完全替换它:

import asyncio
from concurrent.futures import ProcessPoolExecutor


def do_proc_work(stuff, stuff2):  # This runs in a separate process
    return stuff + stuff2

@asyncio.coroutine
def do_work():
    out = yield from loop.run_in_executor(ProcessPoolExecutor(max_workers=1),
                                          do_proc_work, 1, 2)
    print(out)

if __name__  == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(do_work())

输出:

3

如果您绝对需要multiprocessing.Queue,与ProcessPoolExecutor结合使用时似乎会表现良好:

import asyncio
import time
import multiprocessing
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor


def do_proc_work(q, stuff, stuff2):
    ok = stuff + stuff2
    time.sleep(5) # Artificial delay to show that it's running asynchronously
    print("putting output in queue")
    q.put(ok)

@asyncio.coroutine
def async_get(q):
    """ Calls q.get() in a separate Thread. 

    q.get is an I/O call, so it should release the GIL.
    Ideally there would be a real non-blocking I/O-based 
    Queue.get call that could be used as a coroutine instead 
    of this, but I don't think one exists.

    """
    return (yield from loop.run_in_executor(ThreadPoolExecutor(max_workers=1), 
                                           q.get))

@asyncio.coroutine
def do_work(q):
    loop.run_in_executor(ProcessPoolExecutor(max_workers=1),
                         do_proc_work, q, 1, 2)
    coro = async_get(q) # You could do yield from here; I'm not just to show that it's asynchronous
    print("Getting queue result asynchronously")
    print((yield from coro))

if __name__  == "__main__":
    m = multiprocessing.Manager()
    q = m.Queue() # The queue must be inherited by our worker, it can't be explicitly passed in
    loop = asyncio.get_event_loop()
    loop.run_until_complete(do_work(q))

输出:

Getting queue result asynchronously
putting output in queue
3

答案 2 :(得分:0)

aiopipe(https://pypi.org/project/aiopipe/)看起来像是砸在这里的头上。

至少对我有帮助。