1
我修改了示例隊列,生產者&消費者來自這個Tornado documentation,但它似乎並沒有傳遞給get()的超時參數可以工作,因爲消費者不會等待10拋出異常之前的幾秒鐘。理想情況下,生產者和消費者將同時運行。另外,我不知道是否在超時參數爲秒或毫秒傳:龍捲風異步隊列不等待
from tornado import gen
from tornado.ioloop import IOLoop
from tornado.queues import Queue
q = Queue()
@gen.coroutine
def consumer():
try:
while True:
item = yield q.get(timeout=10000)
try:
print('Doing work on %s' % item)
finally:
q.task_done()
except gen.TimeoutError:
print('timeout')
return
@gen.coroutine
def producer():
for item in range(5):
yield q.put(item)
print('Put %s' % item)
yield gen.sleep(2)
@gen.coroutine
def main():
# Start consumer without waiting (since it never finishes).
IOLoop.current().spawn_callback(consumer)
yield producer() # Wait for producer to put all tasks.
yield q.join() # Wait for consumer to finish all tasks.
print('Done')
IOLoop.current().run_sync(main)
,這裏是它的執行:
Put 0
Doing work on 0
timeout
Put 1
Put 2
Put 3
Put 4