Skip to content

nfo94/asyncio-basics

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 

Repository files navigation

asyncio basics in Python

This repository is for study purposes. Is a high level overview of the asyncio docs focusing on methods that I'm using at work.

Overview of concurrency, using Python as language:

concurrency

πŸ“Œ Process:

πŸ“Œ Thread:

πŸ“Œ I/O bound:

πŸ“Œ CPU bound:

πŸ“Œ Concurrency:

πŸ“Œ Parallelism:

πŸ“Œ GIL:

πŸ“Œ Because of Python's GIL, we still won't have a process running more than one thread at the same time, since the threads needs to acquire the GIL one at a time.

πŸ“Œ The event loop is a core concept in asynchronous programming, a design pattern, acting as the central executor in an asyncio-based application. It manages and distributes the execution of different tasks, allowing for asynchronous tasks and callbacks to be executed, network IO operations to be performed, and subprocesses to be run. It works by continuously cycling through pending tasks, executing them, and then waiting for further tasks or IO events. This mechanism allows for non-blocking concurrent execution, making it possible to handle high levels of IO-bound workloads efficiently.

πŸ“Œ A coroutine in Python is a function defined with async def and is designed to be used with asynchronous operations. It can be paused and resumed at await points, allowing other code to run during its idle time. Coroutines are a fundamental part of Python's asyncio library, enabling concurrent execution of code. They are used for IO-bound and high-level structured network code.

πŸ“Œ "asyncio exploits the fact that I/O operations release the GIL to give us concurrency, even with only one thread. When we utilize asyncio we create objects called coroutines. A coroutine can be thought of as executing a lightweight thread. Much like we can have multiple threads running at the same time, each with their own concurrent I/O operation, we can have many coroutines running alongside one another. While we are waiting for our I/O-bound coroutines to finish, we can still execute other Python code, thus, giving us concurrency. It is important to note that asyncio does not circumvent the GIL, and we are still subject to it."

Excerpt from Python Concurrency with asyncio by Matthew Fowler.

Event loops run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses.

Get the running event loop in the current OS thread:

asyncio.get_running_loop()

This raise a RuntimeError if there is no running event loop. and can only be called from a coroutine or a callback.

Get the current event loop:

asyncio.get_event_loop()

Because this function has rather complex behavior using the get_running_loop() function is preferred to get_event_loop() in coroutines and callbacks.

Obs.: get_event_loop() is deprecated.

Set loop as the current event loop for the current OS thread:

asyncio.set_event_loop()

Create and return a new event loop object:

asyncio.new_event_loop()

Running and stopping the loop

Run until the future (an instance of Future) has completed:

loop.run_until_complete(future)

If the argument is a coroutine object it is implicitly scheduled to run as a asyncio.Task. Return the Future’s result or raise its exception.

πŸ“Œ A Future represents an eventual result of an asynchronous operation. Not thread-safe. Future is an awaitable object. Coroutines can await on Future objects until they either have a result or an exception set, or until they are cancelled. A Future can be awaited multiple times and the result is same.

Run the event loop until stop() in called:

loop.run_forever()

Stop the event loop:

loop.stop()

Return True if the event loop is currently running:

loop.is_running()

Return True if the event loop was closed:

loop.is_closed()

Close the event loop:

loop.close()

The loop must not be running when this function is called. Any pending callbacks will be discarded. This method clears all queues and shuts down the executor, but does not wait for the executor to finish.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors