What is an async function?

An async function or coroutine is an special kind of function which allows you to work with events that happend without your control but you need to access to some of the information generated on that.

For example, if you realize a query to a server you can’t control how much time last the work of the computer searching the data and returning to you, so you have to await for the data. But, what can you do with this awaited time. In the classic paradigm you have to wait for the returning data, but with the asynchronous paradigm you can do something else.

With that in mind you can use this tools to message exchange in any case, socket communications (to databases, other servers, etc) and any system with multiple events happening at same time.

In Python the library async provides an implementation for that purpose, and you have to consider some new concepts and stages to enable the execution of this coroutines.

The classic way

The classic way or synchronous programming way, you have a number of actions that the computer runs one after other, you can’t attend other events.

../_images/lineal.png

In python the GIL(Global Interpreter Locking) take charge of that and blocks any other action while the code is running.

The asynchronous way

With that programming style we can say the we are near reality, on our programs we depends from things that can or can’t happend without our control, but we can give some confidence to some systems or the systems that we know how works (in general), and then give or send, and get or receive data from them.

../_images/async_progm.png

So, when we detect that something is ready to proceed, we continue with that. The order whem that happen doesn’t matter.

Better explained:

../_images/eventloop.png

How to Create an Asyncio Coroutine

Look at this definition:

1
2
3
4
5
6
 import asyncio

 async def holacoro():
   for i in range(3):
     await asyncio.sleep(1)
     print("Hola %d" % i)
  1. Import the module
  2. Create a new function with async
  3. Use the wait

How use the coroutine in a simple way

Look at this code:

loop = asyncio.get_event_loop()
#creamos tarea y la asociamos al loop, ejecutandola
loop.run_until_complete(holacoro())

The loop is an object that manage the exuction of the coroutines

Wait a moment, how it’s work?

Well, step by step:

  • Define the coroutine
  • Create a Future Task in what te coroutine vill be run
future_task = holacoro()
  • Collect other different tasks. Other coroutines will be run.
future_task_2 = othercoro()
  • Create a group, set or list
task_list = [future_task, future_task_2]
  • Gather the tasks

The tasks are the arguments from the unpacked list of pack to create the gather object.

tasks_gathered = asyncio.gather(*tasks)
  • Finally, run the gathered tasks.
loop.run_until_complete(tasks_gathered)

Main features

  1. The eventloop with many different implementations, the standar and others (like quamash for qt)
  2. Abstractions for layers Transport and Protocol, (OSI model?)
  3. Support for networking uses: TCP, UDP, UnixSocket, SSL, subprocesses, pipes, etc.
  4. Sintaxis based on (async, await) pair.
  5. Manage Futures and coroutines
  6. Interface to work with threads and multiprocessing

Consider this objects when work with coroutines

  1. Eventloop: the loop manager
  2. Coroutines: the special function
  3. Futures, Tasks: when you say you will run some coroutine
  4. Streams: comunication elements
  5. Subprecesses: activate different workes
  6. Queues: to share data among processes