current position:Home>Understand the basis of Python collaboration in a few minutes

Understand the basis of Python collaboration in a few minutes

2022-02-01 01:39:49 Pumping menstruation without drinking AD calcium

Coroutines is python An unavoidable Foundation , Because of the existence of synergy ,Python It has greatly improved the execution efficiency . The co word of the co process , With the meaning of cooperation , Suppose there is only one thread , We want to run multiple functions , The normal process is running one by one , But this efficiency is too low , Take a very simple example , If there are three functions , The first function runs 3 minute , The second function runs 2 minute , Third run 1 minute , So running down requires 6 minute , But using a collaborative process is as long as 3 Minutes. OK, Because just take the maximum , Without the need to sum . If the function here doesn't have cpu Computing intensive , But one that requires only a small amount of computing resources , But it takes time , Examples like this are common , Like network requests , In fact, we don't have much computing resources in the computer , And a lot of time is waiting , So if we can collaborate in the middle of the waiting process , You can use this thread to calculate other functions . We can look at a very simple example :


import time
import asyncio

async def eat():
    await asyncio.sleep(3)
    print(' having dinner ')
async def sleep():
    await asyncio.sleep(2)
    print(' sleep ')

async def dadoudou():
    await asyncio.sleep(1)
    print(' Doudou ')

loop=asyncio.get_event_loop()
tasks=asyncio.gather(eat(),sleep(),dadoudou())
start=time.time()
loop.run_until_complete(tasks)
print('total spend time is {:.2f}s'.format(time.time()-start))
loop.close()
 Copy code 

The results of the run are as follows :

 Doudou 
 sleep 
 having dinner 
total spend time is 3.00s
 Copy code 

We can find out , Our program only uses 3 The second is over , Not the need to 6 second , This is the wonderful place of synergetic process , How does it work ? In the above program, we use a function asyncio.gather(eat(),sleep(),dadoudou()) Wrap it up , This function is more than just a wrapper , Fortunately, in the internal process task scheduling , I found three in the scheduling awiat key word , It can know that three processes need to be run , The process will be allocated automatically , Then until we call loop.run_until_complete(tasks), The program will directly enter the running state , And will run these three at the same time await, In this way, asynchronous calculation can be achieved . This example is very simple , We can look at an example diagram :

image.png We can see that using a coroutine only needs to find the maximum calculation time as the total running time of the program , Instead of using a co process, it's summation , So the use of CO process 3 second , Not using is 6 second .

But here's the thing , Synergy does not improve cpu Efficiency of calculation , No matter in io Intensive or cpu intensive , The essence of collaborative process cannot be improved , Just improve the waiting time of the program , And compared to threads , It's much faster to switch directly , Otherwise, this thing might as well use threads directly , The thread is in Python The switching efficiency is very low , Therefore, it is necessary to have a collaborative process to replace some use scenarios .

copyright notice
author[Pumping menstruation without drinking AD calcium],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202010139467984.html

Random recommended