multiprocessing.Queue 可以与 gevent 一起使用吗?
有人知道这段代码有什么问题吗?它只是永远“加载”。无输出。 “Sites”是一个由几十个字符串组成的列表。
num_worker_threads = 30
def mwRegisterWorker():
while True:
try:
print q.get()
finally:
pass
q = multiprocessing.JoinableQueue()
for i in range(num_worker_threads):
gevent.spawn(mwRegisterWorker)
for site in sites:
q.put(site)
q.join() # block until all tasks are done
Anyone know what is wrong with this code? It simply "loads" forever. No output. "Sites" is a list of a few dozen strings.
num_worker_threads = 30
def mwRegisterWorker():
while True:
try:
print q.get()
finally:
pass
q = multiprocessing.JoinableQueue()
for i in range(num_worker_threads):
gevent.spawn(mwRegisterWorker)
for site in sites:
q.put(site)
q.join() # block until all tasks are done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
gevent.spawn() 创建 greenlet 而不是进程(甚至更多:所有 greenlet 都在单个操作系统线程中运行)。所以
multiprocessing.JoinableQueue
在这里不合适。gevent
基于协作多任务处理,即,除非您调用切换到gevent
事件循环的阻塞函数,否则其他 greenlet 将不会运行。例如,下面的 conn 使用修补过的 gevent 套接字方法,允许其他 greenlet 在等待站点回复时运行。如果没有pool.join()
将控制权交给运行事件循环的 greenlet,则不会建立连接。要在向多个站点发出请求时限制并发,您可以使用 gevent.pool.Pool:
gevent.spawn()
creates greenlets not processes (even more: all greenlets run in a single OS thread). Somultiprocessing.JoinableQueue
is not appropriate here.gevent
is based on cooperative multitasking i.e, until you call a blocking function that switches togevent
's event loop other greenlets won't run. For exampleconn
below uses patched for gevent socket methods that allow other greenlets to run while they wait for a reply from the site. And withoutpool.join()
that gives up control to the greenlet that runs the event loop no connections will be made.To limit concurrency while making requests to several sites you could use
gevent.pool.Pool
:使用
gevent.queue.JoinableQueue
反而。绿色线程(gevent
内部使用它)既不是线程也不是进程,而是带有用户级调度的协程。Use
gevent.queue.JoinableQueue
instead. Green threads (gevent
internally uses it) are neither threads nor process, but coroutine w/ user-level scheduling.