import logging
import asyncio
q = asyncio.Queue()
async def a_difficult_task(some_arg):
print(f"task {some_arg} started")
await asyncio.sleep(120)
print(f"task {some_arg} ended")
async def worker():
while True:
try:
some_arg = await q.get()
await a_difficult_task(some_arg=some_arg)
except (KeyboardInterrupt, SystemExit, SystemError):
break
except Exception:
logging.exception("...")
finally:
q.task_done()
async def main():
asyncio.create_task(worker())
print("Adding tasks to queue")
for x in range(1, 7):
await q.put( x )
print("Tasks added to queue")
while True:
await asyncio.sleep(1)
if __name__ == "__main__":
asyncio.run(main())