async queue hangs when used with background thread

Viewed 1056

It seems asyncio.Queue only can be pushed by the same thread reading it? For instance:

import asyncio
from threading import Thread
import time

q = asyncio.Queue()

def produce():
    for i in range(100):
        q.put_nowait(i)
        time.sleep(0.1)

async def consume():
    while True:
        i = await q.get()
        print('consumed', i)

Thread(target=produce).start()
asyncio.get_event_loop().run_until_complete(consume())

only prints

consumed 0

and then hangs. What am I missing?

1 Answers
Related