将多处理队列/字典/等等传递给绿色线程 [英] Passing a multiprocessing queue/dictionary/etc.. to green threads
问题描述
将多处理对象(队列,字典等)传递给多个gevent线程是否安全?由于它们实际上不是并发运行的,所以我认为没有问题.但是,我知道gevent不应该与多处理特别兼容.
Is it safe to pass a multiprocessing object (queue, dictionary, etc...) to multiple gevent threads? Since they're not actually running concurrently, I don't think there's a problem. However, I know that gevent isn't supposed to be specifically compatible with multiprocessing.
推荐答案
不幸的是,目前看来 gevent
与 multiprocessing
中的对象不兼容:
Unfortunately, it seems like, at the moment, gevent
is not compatible with objects from multiprocessing
:
这很危险.
mp.Queue
和其他mp数据结构在内部利用信号量之类的东西: https://github.com/python/cpython/blob/master/Lib/multiprocessing/queues.py#L48
It is dangerous. The
mp.Queue
and other mp data structures utilize things like Semaphores internally: https://github.com/python/cpython/blob/master/Lib/multiprocessing/queues.py#L48
Linux下的信号量不是基于fd的,因此需要线程包装器来解除阻塞主循环线程.一般来说,如果事情往南走,则可以用信号量完全阻塞主线程,以无限期地等待某个事件的发生.
Semaphores under Linux are not fd-based and would require threaded wrappers to unblock the main loop thread. Generally speaking, if things go south, it's possible to completely block the main thread with a semaphore waiting infinitely for some event to happen.
(引自GitHub问题 https://github.com/gevent/gevent/issues/1443 )
(Quote from GitHub issues https://github.com/gevent/gevent/issues/1443)
这篇关于将多处理队列/字典/等等传递给绿色线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!