Asynchronous Programming in Python
Before going through the article let’s talk about, what does “asynchronous programming ” means a program writing to a log file, in simple words it means that the program doesn’t wait for a particular process to complete, but carries on regardless.
The execution of such program is little bit faster, as your logging code should be written so that if it does fill the disk, it just stops logging rather than crashing. The code usually involves threading if it’s a single core; with multi-core, it might get handled by another process running on a different core.
Running a single core can only read one set of instructions at a time and execute, whereas with multi-core you can switch to another, that’s analogous to threaded execution. A single core can be processed in different speeds, where it appears to do both the jobs simultaneously. It does both computation plus reading a file from disk
Asynchronous python previously was asyncio (sometimes written as async IO), which is a concurrent programming design in Python, there were generator-based co-routines; Python 3.10 removes those. The asyncio module was added in Python 3.4, followed by async/await in 3.5.
There are 2 concepts of Asynchronous ;
a. Coroutine is a function with an async definition.
b. It can also be an object returned from a coroutine function.
c. It can be called as awaitstatement
d. where the program will run up to the await statement, call the function, and suspend execution until the function completes; other coroutines now get a chance to run.
e. suspension of execution means control is returned to the event loop.
f. When you use asyncio, an event loop runs all the asynchronous tasks, performs network IO and runs sub-processes.
g. mostly when you write coroutines, you will use tasks to run them.
a. It simplifies managing the execution of several coroutines.
b. It lets you run a coroutine in an event loop.