- 2 mins

Asyncio Concepts You Should Know Before Using It!

Table of Contents

2. Even Loop

Even loop is neither a process nor a thread, but rather an event loop, which is a lightweight mechanism for managing and scheduling coroutines and tasks in a single thread.

The event loop is responsible for scheduling and executing coroutines and tasks, which are Python functions that can be paused and resumed in a non-blocking way. When a coroutine encounters an await statement, it yields control back to the event loop, which can then resume another coroutine or perform other processing until the awaited operation is ready to resume.

The event loop is implemented as a class called AbstractEventLoop, which is the base class for all event loop implementations.

But the event loop is more complex system that includes various components and mechanisms for managing and scheduling coroutines, handling I/O operations, and processing system events that AbstractEventLoop in asyncio library does not define. These components and mechanisms typically implemented in the concrete event loop classes, such as asyncio.SelectorEventLoop, asyncio.ProactorEventLoop, or asyncio.DefaultEventLoopPolicy.

For example:

Overall, while the AbstractEventLoop class provides a basic framework for implementing an event loop, the specific components and mechanisms are typically implemented in the concrete event loop classes, which may vary depending on the underlying operating system and other factors.

Demo of create a instance of Even Loop

import asyncio
def run_long_task():
    await asyncio.sleep(10)


@app.route('/long_task')
async def long_task_api():
    loop = asyncio.new_event_loop() # create a new event loop instance  
    asyncio.set_event_loop(loop)  # set the event loop for the current thread
    loop.create_task(run_long_task()) # run the coroutine using the new event loop

Note

- Mechanism: a specific method or process used to achieve a particular result or objective

- coroutine: special type of Python function that can be paused and resumed at certain points during its execution, allowing other code to run in the meantime.  Coroutines manages by event loop inclduing scheduling and execution, as well as I/O operations and other system events.

2. Method introduction

Topaz

Topaz

Always keep learning.

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin google pinterest medium vimeo