我采用了janus库中的代码。我想在现有代码中添加两个功能。首先是批处理,它本身可以正常工作。其次是通过将项目放回队列来重试。问题是我添加的将项目重新注入到队列中的if
块一直在阻塞代码。关于异步队列,我在这里缺少什么?
import asyncio
import janus
loop = asyncio.get_event_loop()
queue = janus.Queue(maxsize=10, loop=loop)
def threaded(sync_q):
for i in range(100):
sync_q.put(i)
print(f"Put {i} into queue")
sync_q.put(None) # queue end signal
sync_q.join()
async def async_batch_coro(async_q, batch_size=3):
while True:
batch = []
for _ in range(batch_size):
val = await async_q.get()
if val is not None:
batch.append(val)
else:
async_q.task_done()
break
for i in batch:
# If I remove the `if` block here, code works just fine with batching
if i % 11 == 0:
await async_q.put(i + 1)
print(f"Put {i} back into queue")
async_q.task_done()
print(f"Received: {batch}")
if len(batch) < batch_size:
break
fut = loop.run_in_executor(None, threaded, queue.sync_q)
loop.run_until_complete(async_batch_coro(queue.async_q))
loop.run_until_complete(fut)