0

I started reading about Async IO in Python, and I found different initialization of the event cycle in different sources in the examples.

One example uses:

await asyncio.gather(
    asyncio.create_task(task("One", work_queue)),
    asyncio.create_task(task("Two", work_queue)),
)

Another example uses:

ioloop = asyncio.get_event_loop()
tasks = [ioloop.create_task(foo()), ioloop.create_task(bar())]
wait_tasks = asyncio.wait(tasks)
ioloop.run_until_complete(wait_tasks)

It is explained here that gather is used mainly for high-level manipulation.

As I understood the key concepts in this question are: future objects and low/high-level async/await code. Judging by the documentation, gather works with the list as a whole. According to the function asyncio.get_running_loop() in the documentation, the information is incomprehensible to me.

But I still can't grasp the conceptual difference. Or is it enough to remember that the first option is for simplified tasks, and the second one is for more precise settings?

CDspace
  • 2,639
  • 18
  • 30
  • 36
febqij
  • 3
  • 1
  • [Python Asyncio: The Complete Guide](https://superfastpython.com/python-asyncio/) includes a discussion of the differences between the two. My take from a quick gloss is that the task API gives you more control over when the asynchronous code runs. Let us know if the guide is useful to you. – The Lazy Graybeard Nov 17 '22 at 14:58
  • @TheLazyGraybeard Thanks a lot for the link, sorry I didn't answer right away, the material is pretty big. Everything is described in detail and clearly on this source, it's great. – febqij Nov 22 '22 at 09:06

0 Answers0