1

イベントレットパッケージを使用してマルチコルーチンタスクを実行すると、コルーチンプールが空の場合でも、プログラムは実行を継続しませんが、ループでスタックします。以下は私のコードであり、最後の行は実行されません。

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
4

1 に答える 1

1

q.get() への最後の呼び出しが永久にブロックされ、何かがキューに追加されるのを待っているため、最後の行は決して実行されません。get にタイムアウト値を渡すなど、いくつかの方法でこれを修正できます。最もクリーンな解決策は、ループの別の反復を再試行する前に、キューが空の場合に現在のタスクが終了するのを待つことだと思います。

while not q.empty():
    pool.spawn_n(download, q.get())
    if q.empty(): pool.waitall()
于 2011-06-22T12:26:58.303 に答える