In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail how to use the Python asyncio module. The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use the Python asyncio module" can help you solve your doubts.
Basic knowledge of Python and asyncio
Coroutine, also known as microthreading, is another way to implement multitasking. It is a smaller execution unit than a thread, usually running on a single process and a single thread. Because it comes with the context of CPU, it can switch tasks through simple event cycles, which is more efficient than the switching of processes and threads, because the switching of processes and threads is carried out by the operating system.
Python implements the cooperative program mainly with the help of two libraries: asyncio and gevent. Since asyncio has become the standard library of python and can be used without pip installation, this means that asyncio will be more popular as a native implementation of Python protocols. This article will only introduce the asyncio module. If you have a need for gevent, please leave a message and I will write a separate article about the use of this library.
Asyncio is a standard library introduced from Python3.4, which has built-in support for asynchronous IO. The programming model of asyncio is essentially a message loop. We generally define a co-program function (or task), get the event loop loop from the asyncio module, and then throw the cooperative tasks (or task list) that need to be executed into the loop to implement asynchronous IO.
Define the evolution of co-programming functions and execution methods
In the earliest Python 3.4, co-programming functions were implemented through @ asyncio.coroutine and yeild from, as shown below.
Import asyncio @ asyncio.coroutine def func1 (I): print ("coprogramming function {} starts execution immediately." .format (I)) yield from asyncio.sleep (2) print ("coprogramming function {} finished!" .format (I)) if _ _ name__ = ='_ _ main__': # get event loop loop = asyncio.get_event_loop () # execute collaborative task loop.run_until_complete (func1 (1)) # close event loop loop.close ()
Here we define a func1 co-program function, which we can use asyncio.iscoroutinefunction to verify. After defining the co-program function, we first get the event loop loop, use its run_until_complete method to execute the co-program task, and then close the loop.
Print (asyncio.iscoroutinefunction (func1 (1) # True
After Python 3.5, the async/await syntax was introduced to define the co-program function, as shown below. Each co-program function is declared as async to distinguish it from ordinary functions. For time-consuming code or functions, we use await declaration to indicate that it hangs while waiting to switch to another task.
Import asyncio # this is a co-programming function async def func1 (I): print ("co-programming function {} starts execution immediately." .format (I)) await asyncio.sleep (2) print ("coprogramming function {} finished!" .format (I)) if _ _ name__ = ='_ _ main__': # get event loop loop = asyncio.get_event_loop () # execute collaborative task loop.run_until_complete (func1 (1)) # close event loop loop.close ()
Prior to Python 3.7, the execution of cooperative tasks was carried out in three steps, and the code was somewhat redundant. Python 3.7provides a simpler asyncio.run method, and the above code can be simplified to:
Import asyncio async def func1 (I): print (f "coprogramming function {I} starts execution immediately.") Await asyncio.sleep (2) print (f "coprogram function {I} execution complete!") If _ _ name__ ='_ _ main__': asyncio.run (func1 (1))
Note: Python has been able to use f-string to format strings since version 3.6, which is equivalent to a simplified version of the format function.
Create the evolution of collaborative tasks
In the previous example, we only performed a single collaborative task (function). In practical application, we first create collaborative tasks by the co-programming function, then add them to the list of collaborative tasks, and finally hand them over to the event loop for execution.
There are several ways to create a co-program task according to the co-program function, the latest of which is the asyncio.create_task method provided by Python version 3.7, as shown below:
# method 1: use ensure_future method. Future represents an object, an unperformed task. Task1 = asyncio.ensure_future (func1 (1)) task2 = asyncio.ensure_future (func1 (2)) # method 2: use the loop.create_task method task1 = loop.create_task (func1 (1)) task2 = loop.create_task (func1 (2)) # method 3: use the asyncio.create_task method task1 = asyncio.create_task (func1 (1)) task2 = asyncio.create_task (func1 (2)) provided by Python 3.7
After creating multiple collaborative task lists, we also use the asyncio.wait method to collect collaborative tasks and hand them over to the event loop for execution.
Import asyncio async def func1 (I): print (f "coprogramming function {I} starts execution immediately.") Await asyncio.sleep (2) print (f "coprogram function {I} execution complete!") Async def main (): tasks = [] # create a list of 4 collaborative tasks for i in range (1,5): tasks.append (asyncio.create_task (func1 (I)) await asyncio.wait (tasks) if _ _ name__ = ='_ main__': asyncio.run (main ()
As the results are shown below, you will find that the four collaborative tasks are not executed sequentially.
For collecting multiple co-programming tasks, Python also provides a new asyncio.gather method, which works like the asyncio.wait method, but is more powerful. If the task passed in the list is not a collaborative task created by the create_task method, it automatically encapsulates the function into a collaborative task, as shown below:
Import asyncio async def func1 (I): print (f "coprogramming function {I} starts execution immediately.") Await asyncio.sleep (2) print (f "coprogram function {I} execution complete!") Async def main (): tasks = [] for i in range (1,5): # the cooperative task tasks.append (func1 (I)) is not created by the co-program function. # Note here *. Gather automatically encapsulates the function list into a collaborative task. Await asyncio.gather (* tasks) if _ _ name__ ='_ _ main__': asyncio.run (main ()) gets the execution result of the cooperative task
Yes, the gather method has the ability to encapsulate functions as co-programming tasks, but this is not the main difference between the two. The bigger difference between the two is in the processing of the returned results after the execution of the collaborative task. Getting the results of a task execution is usually critical to a program, so it is necessary to spend more time learning more about the use of these two methods.
Asyncio.wait returns two values: done and pending,done are the list of completed collaborative tasks, and pending is the category of timed-out uncompleted collaborative tasks. You can obtain the results returned by each collaborative task through the task.result () method, while asyncio.gather returns the result of all completed collaborative tasks, so you can get all the results without further calls or other operations.
Let's look at two examples. Now modify our co-program function to add a return value to it through return.
Get the execution result of cooperative task through asyncio.wait
Import asyncio async def func1 (I): print (f "coprogramming function {I} starts execution immediately.") Await asyncio.sleep (2) return i async def main (): tasks = [] for i in range (1,5): tasks.append (asyncio.create_task (func1 (I) # gets the result of task execution. Done, pending = await asyncio.wait (tasks) for task in done: print (f "execution result: {task.result ()}") if _ _ name__ = ='_ main__': asyncio.run (main ())
The result of the execution is shown below. You can see that the execution results of collaborative tasks are not returned in the order in which the tasks were added.
Get the execution result of cooperative task through asyncio.gather
Continue to modify our code:
#-*-coding:utf-8-*-import asyncio async def func1 (I): print (f "the coprogram function {I} starts execution immediately.") Await asyncio.sleep (2) return i async def main (): tasks = [] for i in range (1,5): tasks.append (func1 (I)) results = await asyncio.gather (* tasks) for result in results: print (f "execution result: {result}") if _ _ name__ = ='_ main__': asyncio.run (main ())
The result of the execution is shown below. The execution result of the collaborative task is exactly the same as the order in which the task is added.
Now do you know the real difference between the gather and wait methods?
Gather has the ability to package ordinary co-programming functions as co-programming tasks, but wait does not. Wait can only accept the packaged task list as parameters.
The two return values are different. Wait returns a list of completed and unfinished tasks, while gather directly returns the execution result of the collaborative task.
The execution result of the task returned by gather is ordered, and the result obtained by the wait method is unordered.
Advanced usage of asyncio
Add a callback function to a task
We can also add a callback function to each task through the add_done_callback method to a single task, as shown below:
#-*-coding:utf-8-*-import asyncio async def func1 (I): print (f "the coprogram function {I} starts execution immediately.") Await asyncio.sleep (2) return I # callback function def callback (future): print (f "execution result: {future.result ()}") async def main (): tasks = [] for i in range (1,5): task = asyncio.create_task (func1 (I)) # Note here Add callback function task.add_done_callback (callback) tasks.append (task) await asyncio.wait (tasks) if _ _ name__ ='_ main__': asyncio.run (main ()) set task timeout
Many collaborative tasks are time-consuming. When you use the wait method to collect collaborative tasks, you can use the timeout option to set the maximum waiting time for a single task before switching, as shown below:
# get the result of task execution, as shown below: done,pending = await asyncio.wait (tasks, timeout=10) introspection
Asyncio.current_task: returns the currently running Task instance, or None if there are no running tasks. If loop is None, get_running_loop () is used to get the current event loop.
Asyncio.all_tasks: returns a collection of incomplete Task objects that the event loop runs.
After reading this, the article "how to use the Python asyncio module" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about the article, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.