Python callback function for asynchronous processing

Speaking of asynchronous processing, everyone should think of Ajax processing, so let's talk about what an Ajax request is first.

 

Ajax is equivalent to simulating an information sending request. You can find it when registering on many websites. For example, if you enter "123" in the user name, then it may prompt you that the user already exists, and it gives you the feeling that the page does not exist. If it is not refreshed, that is, the form is not submitted, and the user name is stored in the database, that is to say, to query whether the user name exists, you must send the user name in the form, and then query in the database.

 

And this process is processed by Ajax. The user enters the user name, and when the focus of the form changes, Ajax will be triggered, and then Ajax will send a GET or POST request to the server, and the server will process the passed data!

What I will share with you today is the implementation of asynchronous processing through callback functions in Python.

 

The sample code is as follows:

import threading
import time
import datetime

#第一个请求
def request_1():
    print("the request 1 is start")
    io(callback)
    print("the request 1 is end")

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

#获取数据请求类的操作,如:从db读取数据,循环耗时,调用其他api等
def io(callback):
    def run(call):
        print("the run is start")
        time.sleep(5)
        print("the run is end")
        conn_db=[x for x in range(10000)] #模拟从db获取数据
        call(conn_db)
    # 这里是启动一个线程去处理这个io操作,不用阻塞程序的处理
    threading.Thread(target=run,args=(callback,)).start()

#回调函数
def callback(data):
    print("the callback is start")
    print("the response of callback is:",data)
    print("the callback is end")

if __name__ == '__main__':
    start_time=datetime.datetime.now()
    request_1()
    request_2()
    end_time=datetime.datetime.now()
    #这里是在统计总耗时,从打印的结果可以看到是异步处理的。
    print("the spend of total time is:",(end_time-start_time).seconds)

The output is as follows:

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

Process finished with exit code 0

to sum up:

Exception handling means that when we need to wait for an IO to be processed for a long time, we can do other things that can be handled without waiting in line, which improves the processing efficiency of the system, which is very important for a system.

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/110198739