Python Asynchronous Programming – asyncio and awaitthe 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 TasksAn 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 – 1Output: Waiting 5 seconds. Hello Hello Hello Hello Hello Finished waiting. Explanation – In the above code –
We can also schedule tasks or objects that bind coroutine and help them to run. Let’s understand the following example. Example – 2Output: started at 11:11:54 hello world finished at 11:11:57 Explanation – In the above code,
Now, let’s make some changes to the above code and see the result. Example – 3 Creating TasksOutput: 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 PythonAsyncio 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.
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 PythonThe 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 PythonWe 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.
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.
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.
Some Important Functions in AsyncioBelow are the some essential methods that used while doing asynchronous programming. Running an asyncio Program
Example – Creating Tasks
Example – Sleeping
Example – Timeouts
Example – ConclusionThis 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. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263501.html