Guifan Li
Guifan Li

Reputation: 157

why the iterator is blocking forever when the

I have a piece of code that consists of both a consumer and a producer.

The consumer is basically iterate over a shared queue, however, an interesting thing is that when the consuming speed is faster than that of producing like below, the code below will be blocked at the iterator and the while loop in push_queue is also blocked.

class Ntest(object):
    def __init__(self, q):
        self.queue = q

    async def push_queue(self) -> None:
        counter = 0
        while True:
            self.queue.put(str(counter))
            await asyncio.sleep(2)
            counter = counter + 1

    async def print_queue(self) -> None:
        for elem in iter(self.queue.get, None):
            await asyncio.sleep(1)
            print(elem)

async def main():
    q = queue.Queue()
    ntest = Ntest(q)
    await asyncio.gather(ntest.push_queue(), ntest.print_queue())

if __name__ == '__main__':
    asyncio.run(main())

Could someone can explain to me why it is blocked in this way given I am expecting push_queue will keep pushing item into queue and print_queue will only be blocked when the queue is empty?

Upvotes: 0

Views: 226

Answers (1)

George
George

Reputation: 254

You are using synchronous queue here q = queue.Queue() which leads to blocking. Please use asyncio.Queue https://docs.python.org/3.6/library/asyncio-queue.html to have it working asynchronously

Upvotes: 2

Related Questions