Coroutines and Task

 

    Published Sept. 11, 2024, 7:41 p.m. by frank_casanova  

 

Introducing to coroutines:

Coroutines and Tasks in Python

Coroutines are Python functions that can pause and resume their execution. They are created using the async keyword and can be paused using the await keyword. Coroutines are useful for concurrent programming, which allows multiple tasks to run at the same time.

When a coroutine pauses, it gives control to the event loop, which can then run other tasks. Once the long-running operation that the coroutine is waiting for is complete, the event loop will resume the coroutine.

Coroutines can be used to improve the performance of applications by running multiple time-consuming operations concurrently.

Tasks are wrappers around coroutines that schedule them to run on the event loop as soon as possible. This allows us to execute multiple tasks concurrently, even if they are waiting for long-running operations to complete.

This contrasts with using the await keyword, which blocks the current coroutine until the result of the awaited expression is available.

Once the tasks are scheduled, we can execute other code while they are running. This is because tasks are executed non-blockingly, meaning that they do not block the current coroutine.

Tasks are a powerful tool for concurrent programming in Python. They allow us to execute multiple tasks concurrently, even if they are waiting for long-running operations to complete. This can significantly improve the performance of our applications.

 

Similar posts

AsyncIO-Sockets

AsyncIO-3

Futures

AsyncIO-2

0 comments

There are no comments yet.

Add a new comment