Passing a multiprocessing queue/dictionary/etc.. to green threads

别来无恙 提交于 2020-04-06 03:30:07

问题


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.


回答1:


The benefits would likely be lost, standard threaded queue implements locks where a green thread would likely be slowed down. Thankfully, gevent often has its own but similar constructs. Check out gevent.queue




回答2:


Unfortunately, it seems like, at the moment, gevent is not compatible with objects from multiprocessing:

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

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.

(Quote from GitHub issues https://github.com/gevent/gevent/issues/1443)




回答3:


I would say that it is a thread safe object then it is not dangerous, but you should always think hard about it. If it isn't thread safe you need to worry about reentrancy of the methods and the consequence of the different object operations not being atomic. Some objects are stateful and they need to complete certain operations before another thread comes in.



来源:https://stackoverflow.com/questions/22162538/passing-a-multiprocessing-queue-dictionary-etc-to-green-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!