简体   繁体   中英

How to stop a coroutines / thread in Python eventlet

When I use the eventlet package to run a multi-coroutines task, even when the coroutines pool is empty, the program won't continue to run, but will get stuck in a loop. Following is my code and the last row never get executed.

import eventlet

global count
post_id=[]
last_id=0

def download(post_id):
    global count
    print "coroutines :",post_id
    if count<last_id:
        count=count+1
        q.put(count) # put new coroutines  in the queue


pool = eventlet.GreenPool()
q = eventlet.Queue()

for i in range(100,200):
    post_id.append(i)

for i in range(0,5):
    q.put(post_id[i]) # keep 6 coroutines  in the pool

count=post_id[5]
last_id=200

while not q.empty() or pool.running()!=0:
    pool.spawn_n(download,q.get()) #start corroutines

print "The end" #nerver reach to this line

The last row never gets executed because your final call to q.get() blocks forever, waiting for something to be added to the queue. There are a few ways you could fix this, including passing a timeout value to get. I think the cleanest solution is to wait for the current tasks to finish if the queue is empty before attempting another iteration of the loop again:

while not q.empty():
    pool.spawn_n(download, q.get())
    if q.empty(): pool.waitall()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM