Learn Python Coding
40.9K subscribers
718 photos
37 videos
24 files
532 links
Learn Python through simple, practical examples and real coding ideas. Clear explanations, useful snippets, and hands-on learning for anyone starting or improving their programming skills.

Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
🚀 asyncio.Queue.shutdown() — shutting down handlers without special markers

Starting with Python 3.13, the asyncio.Queue can be closed using shutdown(), without the need to use None or other special values.

import asyncio

async def worker(queue):
while True:
try:
task = await queue.get()
except asyncio.QueueShutDown:
break

print(task)
queue.task_done()

async def main():
queue = asyncio.Queue()
task = asyncio.create_task(worker(queue))

await queue.put("task-1")
await queue.put("task-2")

queue.shutdown()
await queue.join()
await task

asyncio.run(main())

After shutdown(), no new items are added, and existing items are processed. When the queue is empty, get() will raise asyncio.QueueShutDown.

For a forced shutdown, there is shutdown(immediate=True), which clears the queue but may disrupt the normal behavior of join().

#Python #Asyncio #Programming #DevTips #Coding #Tech

✨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk

⭐️ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
❤1