difference between asyncio.Task.all_tasks() vs asyncio.all_tasks()

Viewed 10

Both asyncio.Task.all_tasks() and asyncio.all_tasks() take an Abstract event loop as an argument and return a set of tasks associated with an event loop.

The only difference I have noted so far is that the asyncio.all_tasks() works on python 3.7+ versions, whereas the asyncio.Task.all_tasks() works on python 3.6+ versions.

The Documentation states that:

asyncio.Task.all_tasks()

classmethod all_tasks(loop=None)
     Return a set of all tasks for an event loop.
     By default all tasks for the current event loop are returned.

asyncio.all_tasks()

asyncio.all_tasks(loop=None)
     Return a set of not yet finished Task objects run by the loop.
     If loop is None, get_running_loop() is used for getting current loop.
     New in version 3.7.

My use case is that I want to use either of the above functions in a signal handler to cancel all the tasks and exit cleanly. Since, asyncio.Task.all_tasks() supports python 3.6+, I think it is the better option for my use case.

What exactly is the difference between the above two functions, and when to choose one over the other, and which of these should I choose for my use case?

0 Answers
Related