Python standard module--asyncio

Source: Internet
Author: User

1 Introduction to Modules

The Asyncio module is added as a temporary library in Python version 3.4. This means that the Asyncio module may not be backwards compatible or even deleted in subsequent versions of Python. According to the official Python documentation, Asyncio provides a single-threaded concurrent application architecture through multiplexing IO access on coroutines, sockets, and other resources, running network clients and server services, and other related raw services. This article does not cover all the technical points about the Asyncio module, but you can learn how to use the module and why it is useful.

If you need some technology similar to the Asyncio module in some older versions of Python, you can look at twisted or gevent.

2 Module using 2.1 definition

The Asyncio module provides a framework for event loops. The event loop is waiting for some tasks to occur and then executes the corresponding events. It also handles such things as IO operations or system events. Asyncio There are several ways to implement the loop in practice. The way the module is used by default is the most efficient way to run the operating system. If you want, you can also explicitly choose other event loops. An event loop is when event a occurs, and function B works together.

Imagine a scenario where a server waits for a user to access and request some resources, such as a Web page. If this site is not a very well-known site, the server will be idle for a long time. However, once a user clicks on the site, the server needs to respond. This response is event handling. When a user downloads a webpage, the server checks for and invokes one or more event handles. Once these event handles are handled appropriately, they need to return control to the event loop. In order to complete this task in Python, Asyncio uses the co-process.

A co-process is a special function that returns control to its calling function, but does not lose its state. The process is a consumer function and is an extension of the generator. The biggest advantage of a thread is that it does not require too much memory when executing the process. What you need to note is that when you call a co-function, it does not actually execute. Instead, it will return a Coprocessor object that you can pass to the event loop, which can then be executed immediately or later.

When you're using the Asyncio module, another one you might be doing is the future. A future is an object that can represent the outcome of a task that has not yet ended. Your event loop can look at future objects and wait for them to end. When a future is finished, it is set to completed. The Asyncio module also supports locks and signals.

In the final part of this article, I will refer to task. Task is a framework of the process and a subclass of the future. You can schedule a task in the event loop.

2.2 Async and await

Async and await are the newly added keywords in Python 3.5 that define a native association that is easy to differentiate from the co-based generator. If you want to learn more about async and await, you can read pep 492.

In Python 3.4, you can create a co-process as follows

import [email protected] my_foo (): Yield from func ()

This adorner still works in Python 3.5, but the type of the module is updated, and the co-function can tell you whether the interaction is not a native co-process. Starting with Python 3.5, you can use the syntax of async def to define a co-function, so the above function can be defined as follows

Import Asyncioasync def My_coro (): await func ()

When you define a co-function in this way, you cannot use yield inside the function. Instead, you must use a return or an await statement to return the return value to the caller. You should note that the keyword await can only be used in the async def function.

The keyword async and await can be thought of as an interface in asynchronous programming. The Asyncio module is a framework that can be used for asynchronous programming with Async/await. In fact, there is a project called Curio that confirms the concept that it implements the event loop that uses async/await in the background alone.

2.3 Example of a process

While the description above will give you a lot of background on how the process works, sometimes you just want to see some examples so that you can feel the grammatical form of it and how to combine it. With this in mind, let's start with a simple example.

A very common task is that you want to download a complete file, which may originate from internal resources or the Internet. Of course, you may want to download more than one file. Let's create two threads to accomplish this task.

Import asyncioimport osimport urllib.request async def download_coroutine (URL):     request = urllib.request.urlopen (URL)     filename =  os.path.basename (URL)     with open (filename, "WB")  as file_handle:         while True:             chunk = request.read (1024x768)              if not chunk:                 break             file_handle.write (Chunk)         msg =  "finished  downloading {filename} ". Format (filename = filename)          return msgasyNc def main (URLs):     coroutines = [download_coroutine (URL)  for  url in urls]    completed,pending = awit asyncio.wait ( Coroutines)     for item in completed:         print (Item.result ()) if __name__ ==  "__main__":     urls =  ["Http://www.irs.gov/pub/irs-pdf/f1040.pdf",              "Http://www.irs.gov/pub/irs-pdf/f1040a.pdf",              "Http://www.irs.gov/pub/irs-pdf/f1040ez.pdf",              "Http://www.irs.gov/pub/irs-pdf/f1040es.pdf",              "Http://www.irs.gov/pub/irs-pdf/f1040sb.pdf"]     event_loop = asyncio. Get_event_loop ()     try:        event_loop.run_ Until_complete (main (URLs))     finally:         Event_loop.close ()

In this code, we introduce the modules we need and then create the first one through the async syntax. This process is called download_coroutine, and it uses the Python urllib module to download any URL addresses that are passed to it. When it finishes the task, it will return a corresponding message.

The other process is our main process. It basically gets a list of one or more URL addresses, and then joins them to the queue. We use the wait function of Asyncio to wait for the end of the process. Of course, in order to start these processes, they need to be added to the event loop. We did this in the last place in the code snippet, we first get an event loop and then call its Run_until_complete method. You will notice that we are passing the main process into the event loop. This will run the main process first, and the master will join the second process to the queue and let them run. This is known as the chain Association.

2.4 Scheduling calls

You can also schedule calls to regular functions through an asynchronous event loop. The first way we look at it is call_soon. Method Call_soon basically is to call your callback or event handle as much as possible. It works like a FIFO queue, so if some callbacks take a while to process the task, the other callbacks are deferred until the previous callback ends. Let's take a look at an example.

Import asyncioimport functoolsdef event_handler (Loop,stop = false):     print ("event handler called")     if stop:         print ("Stopping the loop")          Loop.stop () if __name__ ==  "__main__":     loop = asyncio.get_event _loop ()     try:        loop.call_soon ( Functools.partial (Event_handler,loop))         print ("Starting  Event loop ")         loop.call_soon (Functools.partial (Event_ handler,loop,stop = true))         loop.run_forever ()      finally:        print ("Closing event loop" )       &NBsp; loop.close () 

Since the Asyncio function does not accept the keyword, if we need to pass the keyword into the event handle, then we need to use the Functools module. Whenever called, the regular functions we define will print some textual information on the standard output. If you accidentally set the stop variable of this function to true, it will stop the event loop.

The first time we call it, we don't stop the event loop. The second time we call it, we stop the event loop. The reason we stop the event loop is that we put it into run_forever, which sets the time loop to an infinite loop. Once the loop is stopped, we can turn it off. If you run this code, you get the output as shown below,

Starting event Loopevent Handler Calledevent handler calledstopping the Loopclosing event loop

There is also a related function that is Call_soon_threadsafe, as the name implies, it is similar to Call_soon's working mechanism, but it is thread-safe.

If you want to delay the call again for a period of time, you can use the Call_later function. In this example, we can modify the Call_soon function as follows,

Loop.call_later (1,event_handler,loop)

This will delay calling our event handle for 1 seconds before calling it and passing the loop as the first parameter.

If you want to schedule at a specified time in the future, you need to get the time of the loop instead of the computer's time, you can operate as follows,

Current_time = Loop.time ()

Once you do this, you can use the CALL_AT function and then pass the time you want to invoke the event handle to it. Let's see if we want to call our event handle in 5 minutes, and here's how you do it,

Loop.call_at (Current_time + 300,event_handler,loop)

In this example, we use the current time we get, and then add 300 seconds or 5 minutes. With this operation, we delay invoking the event loop for 5 minutes.

2.5 Quests

A task is a subclass of the future and a framework for the co-process. Task allows you to record the time at which the task ends processing. Because the task is the future type, the other process can wait for a task, and you can get the result when the task is finished. Let's look at a simple example.

 import asyncioimport timeasync def my_task (seconds):     print (" This task is take {} seconds to cpmplete ". Format (seconds))      time.sleep (seconds)     return  "task finished" if __name__ = =  "__main__":     my_event_loop = asyncio.get_event_loop ()      try:        print ("task creation started")          task_obj = my_event_loop.create_task (My_task (seconds =  2))         my_event_loop.run_until_complete (task_obj)      finally:        my_event_loop.close ()      print ("The task ' s result was :{}". Format (Task_obj.result ())) 

Here, we create an asynchronous function that takes the number of seconds and the time it will run. This mimics a long-running task. We then created our event loop and created a task object through the Create_task function of the event loop object. The function Create_task accepts the function we want to convert to a task. Then we run the event loop until the task is complete. In the end, once the task is over, we get the result of the task.

With the Cancel method of the task, the task can also be easily canceled. When you want to end a task, call it on it. When a task is canceled while waiting for another operation, the task will report a cancelerror error.

2.6 Summary

Here, you should already know how to work with the Asyncio library. The Asyncio library is very powerful and allows you to do a lot of cool and interesting tasks. You can view http://asyncio.org/, which contains a lot of projects using Asyncio, and can get a lot of inspiration on how to use the Asyncio library. Of course, the official Python document is also a good place to start a Asyncio tour.


Python standard module--asyncio

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.