aio-libs / janus

Thread-safe asyncio-aware queue for Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue Passing Element Into Queue Right Before Co-Routine Completes

SkiBum326 opened this issue · comments

I have two coroutines running. When one of the co-routines is ready to finish, it puts an element into the Janus queue, which is queried by the second co-routine indicating that it's time to terminate.

I've noticed that if I put an element into the queue right before the first co-routine terminates, the element is never received in the second co-routine. If I put the element in sooner, it is received properly.

I'm wondering, is this possibly a bug, or am I missing something obvious?

# outbound_queue is a global variable

        async def handle_input_from_raven_desktop():

		try:
			async for msg in ws:
				logger.info('Message received.')	
		except asyncio.CancelledError:
			logger.info('Websocket cancelled error.')
		finally:
			logger.info('Tearing down websocket connection.')
			await ws.close()
			await outbound_queue.async_q.put(None)

	async def handle_output_to_raven_desktop():
	
		while True:
			command = await outbound_queue.async_q.get()
			if command is None:
				logger.info('Terminating handler for transmitting output commands.')
				break
			

It seems that the issue only arises if I attempt to call put after the asyncio.CancelledError is raised

This is not a bug.

The issue was happening because it appears that the two coroutines were cancelled together. Since I didn't catch the CancelledError in the second coroutine, gather hung indefinitely, leading me to think that the queue wasn't receiving the put element correctly.