Python coroutine achieves asynchrony

Today's topic: I have shared how Python implements asynchronous processing through the use of callback functions. Today we will use a simple example to explain how coroutines implement asynchronous processing.

 

The concept of coroutine

A coroutine, also known as a microthread, is a lightweight thread in a user mode. The coroutine can retain the state of the last call. Each time the process is reentered, it is equivalent to entering the state of the last call. In other words: entering the position of the logic flow when it left last time, when there are a large number of errors in the program When CPU operation (IO) is required, it is suitable for coroutines.

 

Advantages of coroutines

The coroutine has extremely high execution efficiency, because subprogram switching is not thread switching, but controlled by the program itself. Therefore, there is no thread switching overhead.

There is no need for a multi-threaded lock mechanism, because there is only one thread, and there is no conflict of writing variables at the same time. In the coroutine, the shared resources are controlled without locking, and only the state is judged. Therefore, the execution efficiency is much higher than that of multi-threads.

Because the coroutine is executed by a thread, the easiest way to use a multi-core CPU is multi-process + coroutine, which not only makes full use of the multi-core, but also gives full play to the high efficiency of the coroutine.

 

Conditions that constitute a coroutine

  • Concurrency must be implemented in only one single thread

  • Modify shared data without locking

  • Save multiple control flow context stacks in the user program

  • When a coroutine encounters an IO operation, it automatically switches to other coroutines

     

     

Python uses coroutines to achieve asynchrony

import threading
import time
import datetime

#第一个请求
def request_1():
    print("the request 1 is start")
    data=yield io()#定义一个协程处理
    print("the response of callback is:",data)
    print("the request 1 is end")

#第二个请求
def request_2():
    print("the request 2 is start")
    time.sleep(2)#定义耗时2s
    print("the request 2 is end")

#获取数据请求类的操作,如:从db读取数据,循环耗时,调用其他api等
def io():
    def run():
        print("the run is start")
        time.sleep(5)#定义耗时5s
        print("the run is end")
        conn_db=[x for x in range(10000)]#模拟从db获取数据
        try:
            global gen
            gen.send(conn_db)#线程结束时发送数据给request_1函数里面的data=yeild io(),此时request_1被唤醒继续处理
        exceptStopIterationas e:
            print(e)
# 这里是启动一个线程去处理这个io操作,不用阻塞程序的处理,即不影响requst_2的继续处理
        threading.Thread(target=run,).start()

if __name__ =='__main__':
    start_time=datetime.datetime.now()
    global gen
    gen=request_1()#函数的赋值操作,生成一个生成器
    next(gen)#调用next函数,预激协程,执行了request_1()函数,如果io被挂起,就直接执行        request_2()
    request_2()
    end_time=datetime.datetime.now()
#这里是在统计总耗时,从打印的结果可以看到是异步处理的。
    print("the spend of total time is:",(end_time-start_time).seconds)

The output after running the program is:

the request 1is start
the run is start
the request 2is start
the request 2is end
the spend of total time is:2
the run is end
the response of callback is:[0,1,...9999]
the request 1is end

Welcome to pay attention to [The Way of Infinite Testing] public account , reply [receive resources],
Python programming learning resources dry goods,
Python+Appium framework APP UI automation,
Python+Selenium framework Web UI automation,
Python+Unittest framework API automation,

Resources and codes are sent for free~
There is a QR code of the official account at the bottom of the article, you can just scan it on WeChat and follow it.

Remarks: My personal public account has been officially opened, dedicated to the sharing of test technology, including: big data testing, functional testing, test development, API interface automation, test operation and maintenance, UI automation testing, etc., WeChat search public account: "Infinite The Way of Testing", or scan the QR code below:

 Add attention and let us grow together!

Guess you like

Origin blog.csdn.net/weixin_41754309/article/details/110201768