Javatpoint Logo
Javatpoint Logo

Python Asynchronous Programming - asyncio and await

the asyncio module. The asyncio module comes with excellent features that allow us to write more efficient Python asynchronous applications. We will explore how to manage an async event loop in Python. Before dive deep into this topic, let's understand what asynchronous programming is.

What is Asynchronous Programming?

In synchronous programming, the methods are written to perform one task at a time. If a function depends on the other function's output, it has to wait to finish the execution of that function. The program is essentially stopped until the function finishes its execution. It means that one program can get executed at a time.

This slows down the program as it is forced to stop and wait for something to finish. Many processors are available in the system, so it is a waste of resources to do other tasks rather than the ideal sit.

To overcome this, the asynchronous programming concept comes into play. It behaves differently; it also takes one execution at a time. But the system may not wait to finish the execution to move on next step.

It means the processor doesn't sit ideal if the program will perform another task while the previous hasn't yet finished and still running elsewhere.

In this tutorial, we will explain why we need such a type of programming.

What is asyncio - Asynchronous I/O?

An asyncio is a Python library which is used to run the concurrent code using the async/wait. It is a foundation for Python asynchronous framework that offers connection libraries, network and web-servers, database distributed task queues, high-performance, etc.

This module provides the framework that works around the event loop and also take care of such things as I/O and system events.

Coroutines and Tasks

An asyncio is a Python library used to run the concurrent code using the async/wait. It is a foundation for Python asynchronous framework that offers connection libraries, network, web-servers, database distributed task queues, high-performance, etc.

This module provides the framework that works around the event loop and takes care of such things as I/O and system events.

Example - 1

Output:

Waiting 5 seconds. 
Hello
Hello
Hello
Hello
Hello
Finished waiting.

Explanation -

In the above code -

  • We have imported the asyncio module to get access to Python async functionality.
  • Then create a primary() function and write the async keyword in front of that. This will allow the program to run the task asynchronously.
  • We used for loop and called the sleep() method, which forced us to wait 1 second.
  • The program prints "Hello" after one second.
  • Program should have the one .run() function as well as one .main() function.

We can also schedule tasks or objects that bind coroutine and help them to run. Let's understand the following example.

Example - 2

Output:

started at 11:11:54
hello
world
finished at 11:11:57

Explanation -

In the above code,

  • We have imported the asyncio and time module.
  • Then we defined execute() function with the delay and value arguments. It prints the delay time using the sleep() method.
  • In the main() function, we have passed two arguments: the first is delay time, and the second is value to be print.
  • The program starts its execution and prints the exact execution time, print 'hello' then wait for the two seconds and print 'world' and stop.

Now, let's make some changes to the above code and see the result.

Example - 3 Creating Tasks

Output:

started at 15:43:30
hello
world
finished at 15:43:32

We can see that it is 1 second faster than the previous program. The create.task() method will run in the event loop, with its result put in the task. We have scheduled the two tasks and returned them using the await.

Manage an async event loop in Python

Asyncio is also used for managing the async event loop. An event loop is an object that runs async functions and callbacks. When we want to execute the coroutines, the event will be crucial for the asynchronous functions when we run the asyncio.run() method; the event loop object is created automatically. To implement the more advanced server, we will need lower-level access to the event loop. We need to work directly with the event loop's internals.

The event loop comes with the following features.

  • It can register, execute, and cancel delayed calls (asynchronous functions)
  • It can create client and server transports for communication
  • It can create subprocesses and transports for communication with another program.
  • Delegate function calls to a pool of threads.

Let's see the following example.

Example -

Output:

This is a asynchronicity!

The event loop starts by getting asyncio.get_event_loop() , scheduling and running the async task and close the event loop when we done with the running.

Read and Write Data with Stream in Python

The asyncio module offers stream which is used to perform high-level network I/O. It can behave as a server for network requests. It is best for long-running network operations where application block waiting for some other resources to return a result.

There are two classes, StreamReader and StreamWriter of asyncio. These classes are used to read and write from the network at a high level.

To read from network, we need to open the network using the asyncio.open_connection(). The StreamReader and StreamWriter objects function return tuple, we would use .read() and .write() method to each connection.

The asyncio.start_server() method is used to receive the connection from the remote hosts. This function takes a callback function, client_connected_cb as arguments. It is called whenever the function received the request.

Synchronization tasks in Python

We have discussed earlier, Asynchronous program runs separately, but sometimes we would want to communicate with each other. The asyncio module offers us a queue and various other methods to establish the synchronization between the tasks.

Let's understand the following implementation method.

  • Queues - The asyncio queues facilitate asynchronous functions to line up Python objects to be consumed by other async functions. For example - The workload is distributed between the function on its behavior.
  • Synchronization Primitive - The asyncio's features locks, events, conditions, and semaphores act as conventional Python counterparts.

Here, a point should always keep in mind that these methods are not thread-safe. This isn't an issue for async tasks running in the same event loop. But we need to use the thread module to share the information between the tasks.

When to use asynchronous programming?

In the following scenario, we can use asynchronous programming.

  • When we want complete work in quick time.
  • The delay involves waiting for I/O (disk or network) operations, not computation.
  • When many I/O operations are happing at once.

The asyncio module allows us to perform multiple tasks in parallel and iterate through them efficiently, without blocking the rest of the application.

Some of the tasks are given below, which can work well with the asyncio.

  • Web scraping.
  • Network Services (web server and framework)
  • Simultaneous Database

Some Important Functions in Asyncio

Below are the some essential methods that used while doing asynchronous programming.

Running an asyncio Program

  • asyncio.run(coro, *, debug = False) - This function is used to block the execution for delay seconds. It suspends the current task and allows another task to run. The delay is an argument that shows the number of seconds.

Example -

Creating Tasks

  • create_task(coro, *, name = None) - This function wraps the Coroutines into a Task and schedule its execution. It returns the task object.

Example -

Sleeping

  • sleep(delay, result = None, *, loop = None) - This function is used to block the execution for delay seconds. It suspends the current task and allows other task to run. The delay is an argument which shows the number of seconds.

Example -

Timeouts

  • coroutinewait_for(aw, timeout, *, loop = None) - This function is used to wait for the aw (a coroutine automatically schedule as a Task) awaitable to complete with a timeout.

Example -

Conclusion

This tutorial includes the concept of asynchronous programming using the Python asyncio module. The asyncio gives us programmatic control of when context when we use the context switches. That means we can handle many complex issues that occur with threaded programming.

It is a powerful and valuable tool, but only for asynchronous type programming. We have discussed Coroutines and tasks with their respective example. We have also discussed managing the event loop and reading and writing data with stream in Python. It also includes the essential methods.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA