python - Proper way to clear an asyncio queue in python3? -


errors in producer/consumer coroutines may leave items , unfinished tasks in asyncio queue. if error encountered, want stop loop, cancel pending tasks , clear queue. while can finish first 2 things, cannot find easy way clear queue. after reading this answer, come 3 methods:

import asyncio  q=asyncio.queue() in range(5):     q.put_nowait(i) q.get_nowait()  loop=asyncio.get_event_loop()  #this raise error if q cannot join loop.run_until_complete(asyncio.wait_for(q.join(),1))  #method 1 q._queue.clear() q._finished.set() q._unfinished_tasks = 0  #method 2 _ in range(q.qsize()):     q.get_nowait() _ in range(q._unfinished_tasks):     q.task_done()  #method 3 del q q=asyncio.queue() 

so 1 better?


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -