In-depth understanding: callback function

What exactly is the callback function has troubled me for a long time~

I saw the posts of several great gods on Zhihu, and suddenly realized

Author: no.body
Link: https://www.zhihu.com/question/19801131/answer/27459821
Source: Zhihu

Author: Chang Xiling
Link: https://www.zhihu.com/question/19801131/answer/13005983
Source: Zhihu

The first thing to be clear is that functions can also be passed as parameters of functions

Well, with this concept, let's explain what the callback function is all about

First there must be at least 3 types of functions

  • Main function: equivalent to the engine of the entire program, scheduling each function to execute in sequence

  • Callback function: an independent functional function, such as the write file function

  • Intermediate function: a function between the main function and the callback function, registering the callback function, notifying the main function, and acting as a bridge

Next, let's take a look at the sample code:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

# 回调函数1
def callback1(x):
    return x * 2 

# 回调函数2
def callback2(x):
    return x ** 2

# 中间函数
def middle(x, func):
    return 100 + func(x)

# 主函数
def main():

    x = 1 

    a = middle(x, callback1)
    print(a)

    b = middle(x, callback2)
    print(b)

    c = middle(x, lambda x: x + 2)
    print(c)

main()

operation result:

102
101
103

After the code is understood, let's analyze the logic of the code next.

First of all, we need to use a function during the execution of the main function x * 2, and callback1the function provides this function, we call this function 回调函数(As for why it is called "callback function", can it be called something else? In fact, this is just a man-made rule You can also call it "Geek Dianer exclusive function", but then you will ask why it is called "Geek Dianer exclusive function". It must have a name! So it is called " Callback function" is bastard's ass: regulations!).

At this time, we 主函数need to call it, but sometimes we encounter operations that need to write to the hard disk during the development process. At this time, in order to avoid program blocking, we need to use asynchronous I/O. It's just that you write and play by yourself first, and Dad goes to do other things, and you will let me know when you are done. It is because of this mechanism that there must be a "place" for 登记回调函数and 通知主函数执行完成, and this is it 中间函数.

With the above content, we can deduce the execution flow of the callback function:

  1. The main function needs to call the callback function

  2. Intermediate function registration callback function

  3. trigger callback function event

  4. call the callback function

  5. Respond to callback events

There are actually two types of callbacks:阻塞式回调 and 延迟式回调can also be called 同步回调and异步回调

The difference between the two is:

In a blocking callback, the callback function must be called before the main function returns

In deferred callbacks, the callback function may be called after the original function returns

The above examples are all synchronous callbacks. Asynchronous requires the use of concepts such as multi-process, multi-thread, and coroutine. Let’s talk about it next time.

Finally, use an example to illustrate that it is a callback function:

You go to a store to buy something, and it happens that the item you want is out of stock, so you leave your phone number with the clerk, and after a few days, the store has stock, the clerk calls you, and then you receive a call Then went to the store to pick up the goods.

In this example, your phone number is called 回调函数, you leave the phone to the clerk to call 登记回调函数, the store later has the goods, it is called 触发回调事件, the clerk calls you 调用回调函数, and you go to the store to pick up the goods 响应回调事件.

おすすめ

転載: blog.csdn.net/yilovexing/article/details/93192484