Python co process asyncio asynchronous programming notes sharing

  • 2021-11-30 01:02:50
  • OfStack

Directory 1. Event loop 2. Synergetic and asynchronous programming 2.1 Basic use
2.2 await
2.3 Task Object

1. Event loop

It can be understood as an infinite loop, Check the tasks in the task list, if they can be executed, if they can't be executed, then ignore and execute other executable tasks. If IO is over (for example, downloading pictures from Baidu will become executable tasks after downloading), then execute the logic after downloading


# The task here is stateful, for example, the task has been completed or is being executed or is being IO Wait 
 Task list  = [  Mission 1,  Mission 2,  Mission 3,... ]
while True:
     List of Executable Tasks, List of Completed Tasks  =  Go to the task list to check all tasks, and set ' Executable ' And ' Completed ' Returns the task of 
    for  Ready task  in  List of executable tasks :
         Perform a ready task 
    for  Completed tasks  in  List of completed tasks :
         Remove from the task list   Completed tasks 
     If   Task list   Terminate the loop if all the tasks in the 

# When writing a program, you can get and create an event loop through the following code. 
import asyncio
loop = asyncio.get_event_loop()
# Put the task into the task list and let the event loop detect the status of the task ( Whether it can be run, whether IO)
loop.loop.run_until_complete( Mission )

2. Synergetic and asynchronous programming

Synergetic function, defined as a function of the form async def.
Synergetic object, the object returned by calling the Synergetic function ().


#  Definition 1 Individual coequation function 
async def func():
    pass
#  Call the synchronic function and return 1 Individual cooperative object ( Internal code does not execute )
result = func()

2.1 Basic usage

In the program, if you want to execute the internal code of the synergetic function, you need the cooperation of the event loop and the synergetic object to realize it
Example 1:


import asyncio
async def func():
    print(" Synergetic internal code ")
#  Call the synchronic function and return 1 Individual co-process object. 
result = func()
#  Mode 1
# loop = asyncio.get_event_loop() #  Create 1 Event loops 
# loop.run_until_complete(result) #  Submits the synchro as a task to the task list of the event loop, and terminates after the synchro executes. 
#  Mode 2
#  Essential way 1 Yes 1 Like, inside first   Create an event loop   Then execute  run_until_complete , 1 A simple way to write. 
# asyncio.run  Function in  Python 3.7  Add to  asyncio  Module, 
asyncio.run(result)

2.2 await

await + Waitable Objects (Synergetic Objects, Future, Task Objects)
await is a key word that can only be used in the synergy function. It is used to suspend the current synergy (task) when encountering IO operation. In the process of suspending the current synergy (task), the event loop can execute other synergy (task). When the current synergy IO processing is completed, it can switch back to execute the code after await again.


import asyncio
async def func():
    print(" Execute the internal code of the synergetic function ")
    #  Encounter IO Operation suspends the current program (task), and so on IO Continue after the operation is completed. 
    #  When the current program is suspended, the event loop can execute other programs (tasks). 
	#response Yes IO The result obtained after the time-consuming is over 
    response = await asyncio.sleep(2)
    print("IO The request ends with the result: ", response)
result = func()
asyncio.run(result)


Results
Execute the internal code of the synergetic function
The IO request ends and the result is: None
# Returning None here means that this good thing is meaningless. If you download a picture successfully, you will return a result

Example 2:


import asyncio
async def others():
    print("start")
    await asyncio.sleep(2)
    print('end')
    return ' Return value '
async def func():
    print(" Execute the internal code of the synergetic function ")
    #  Encounter IO Operation suspends the current program (task), and so on IO Continue after the operation is completed. When the current program is suspended, the event loop can execute other programs (tasks). 
    response = await others()
    print("IO The request ends with the result: ", response)
asyncio.run( func() )

Implementation results:
Execute the internal code of the synergetic function
start
end
IO the request ends with a return value

Example 3:


import asyncio
async def others():
    print("start")
    await asyncio.sleep(2)
    print('end')
    return ' Return value '
async def func():
    print(" Execute the internal code of the synergetic function ")
    #  Encounter IO Operation suspends the current program (task), and so on IO Continue after the operation is completed. When the current program is suspended, the event loop can execute other programs (tasks). 
    #await Wait for a return value before executing downward 
    response1 = await others()
    print("IO The request ends with the result: ", response1)
    response2 = await others()
    print("IO The request ends with the result: ", response2)
asyncio.run( func() )

Implementation results:
Execute the internal code of the synergetic function
start
end
The IO request ends with a return value
start
end
The IO request ends with a return value

await is used when the next step depends on the result of the previous step, but if there are other tasks, it will still be switched to other tasks to execute

2.3 Task Object

Add multiple tasks to the event loop.
Tasks is used for concurrent scheduling of co-routines, and Task objects are created by asyncio.create_task (co-routine objects), so that co-routines can join the event loop and wait for being scheduled for execution.

In addition to using the asyncio.create_task () function, you can also use the lower-level loop.create_task () or ensure_future () functions.

Manual instantiation of Task objects is not recommended.

Essentially, it encapsulates the synergy object as an task object, and immediately joins the synergy into the event loop, while tracking the status of the synergy.
Note: The asyncio. create_task () function is added in Python 3.7.

Before Python 3.7, you can use the lower-level asyncio.ensure_future () function instead.

Example 1:


import asyncio
async def func():
    print(1)
    await asyncio.sleep(2)
    print(2)
    return " Return value "
async def main():
    print("main Begin ")
    #  Create a synergy and encapsulate the synergy into the 1 A Task Object and immediately added to the task list of the event loop, waiting for the event loop to execute (the default is ready). 
    task1 = asyncio.create_task(func())
    #  Create a synergy and encapsulate the synergy into the 1 A Task Object and immediately added to the task list of the event loop, waiting for the event loop to execute (the default is ready). 
    task2 = asyncio.create_task(func())
    print("main End ")
    #  When executing a program encounters IO Operation, it automatically switches to perform other tasks. 
    #  Here await Is to wait for all the corresponding co-processes to be executed and get the results 
    ret1 = await task1
    ret2 = await task2
    print(ret1, ret2)
asyncio.run(main())

Implementation results:
main Start
End of main
1
1
2
2
Return value Return value

Example 2:


import asyncio
async def func():
    print(1)
    await asyncio.sleep(2)
    print(2)
    return " Return value "
async def main():
    print("main Begin ")
    #  Create a synergy and encapsulate the synergy into the Task Object and added to the task list of the event loop, waiting for the event loop to execute (the default is ready). 
    #  When calling 
    task_list = [
        asyncio.create_task(func(), name="n1"),
        asyncio.create_task(func(), name="n2")
    ]
    print("main End ")
    #  When executing a program encounters IO Operation, it automatically switches to perform other tasks. 
    #  Here await Is to wait for all routines to finish executing and save the return values of all routines to done
    #  If you set timeout Value, it means the maximum number of seconds to wait here, and the returned value of the completed coprocedure is written to the done If it is not finished, write to pending Medium ( For example, the following Timeout=1, The picture to download is two seconds, set to 1 The execution fails in seconds ,done The inside is empty, and pending The execution failed in ) . 
    done, pending = await asyncio.wait(task_list, timeout=None)
    print(done, pending)
asyncio.run(main())

 Implementation results: 
main Begin 
main End 
1
1
2
2
{
   <Task finished name='n1' coro=<func() done, defined at C:/Users/xuan.li/Desktop/unidevopss/py3x64/ibuildmaster/apps/PROD/tests.py:513> result=' Return value '>,
   <Task finished name='n2' coro=<func() done, defined at C:/Users/xuan.li/Desktop/unidevopss/py3x64/ibuildmaster/apps/PROD/tests.py:513> result=' Return value '>
}
 set()

Example 3:


import asyncio
async def func():
    print(" Execute the internal code of the synergetic function ")
    #  Encounter IO Operation suspends the current program (task), and so on IO Continue after the operation is completed. When the current program is suspended, the event loop can execute other programs (tasks). 
    response = await asyncio.sleep(2)
    print("IO The request ends with the result: ", response)
coroutine_list = [func(), func()]
#  Error: coroutine_list = [ asyncio.create_task(func()), asyncio.create_task(func()) ]  
#  You can't directly here  asyncio.create_task Because the Task Join the task list of the event loop immediately, 
#  But at this time, the event loop has not been created, so an error will be reported. 
#  Use asyncio.wait Encapsulate the list as 1 Individual program, and call asyncio.run Implement two co-processes 
# asyncio.wait Internally, it executes for each program in the list ensure_future Encapsulated as Task Object. 
done,pending = asyncio.run( asyncio.wait(coroutine_list) )

The above is the Python asyncio asynchronous programming notes to share the details, more about Python asynchronous programming asyncio, please pay attention to other related articles on this site!


Related articles: