(21)回调函数

回调函数

就是一个参数,将这个函数作为参数传到另一个函数里面.

函数先执行,再执行当参数传递的这个函数,这个参数函数是回调函数

语法:  tp.submit(func,i).add_done_callback(call_back)      定义的call_back函数就是一个回调函数

(1)线程池----->是由子线程实现的

from concurrent.futures import ThreadPoolExecutor
from threading import current_thread as cthread
import time
def func(i):
    print("thread", i, cthread().ident)
    time.sleep(1)
    print("thread %s end" % (i))
    return i * "*"
def call_back(args):
    print("call back : ", cthread().ident)
    print(args)
    print(args.result())
tp = ThreadPoolExecutor(5)
for i in range(20):
    # 当执行完这个线程后 在直接执行call_back这个函数
    tp.submit(func, i).add_done_callback(call_back)
tp.shutdown()
print("主线程", cthread().ident)
View Code

执行结果: 可以看到call back 后面跟的线程号其实就是子线程

thread 0 2356
thread 1 7196
thread 2 8440
thread 3 9656thread
 4 1684
thread 1 endthread 0 end
call back :  7196
*
thread 5 7196

call back :  2356

thread 6 2356
thread 2 end
call back :  8440
**
thread 7 8440
thread 4 end
call back :  1684
****
thread 3 end
call back :  thread9656
 ***
8thread  9 96561684

thread 5 end
call back :  7196
*****
thread 10 7196
thread 6 end
call back :  2356
******
thread 11 2356
thread 7 end
call back :  8440
*******
thread 12 8440
thread 9 end
call back :  9656
*********
thread 13 9656
thread 8 end
call back :  1684
********
thread 14 1684
thread 10 end
call back :  7196
**********
thread 15 7196thread 11 end
call back :  2356
***********
thread 16 2356

thread 12 end
call back :  8440
************
thread 17 8440
thread 13 endthread 14 end
call back :  1684
call back :  9656
*************
thread 18 9656

**************
thread 19 1684
thread 16 end
call back :  2356
****************
thread 15 end
call back :  7196
***************
thread 17 end
call back :  8440
*****************
thread 19 end
call back :  1684
*******************
thread 18 end
call back :  9656
******************
主线程 7736
View Code

(2)进程池----->是由主进程实现的

from concurrent.futures import ProcessPoolExecutor
import os, time

def func(i):
    print("process", i, os.getpid())
    time.sleep(1)
    print("process %s end" % (i))
    return i * "*"

def call_back(args):
    print("call back : ", os.getpid())
    print(args)
    print(args.result())

if __name__ == "__main__":
    tp = ProcessPoolExecutor(5)
    for i in range(20):
        # 当执行完这个线程后 在直接执行call_back这个函数
        tp.submit(func, i).add_done_callback(call_back)
    # tp.shutdown()
    print("主线程", os.getpid())
View Code

执行结果:可以看到call back 后面跟的线程号其实就是主线程

主线程 6012
process 0 9636
process 1 8188
process 2 4664
process 3 1756
process 4 2356
process 0 end
process 5 9636
call back :  6012
<Future at 0x2d063c8 state=finished returned str>

process 1 endprocess 2 end

process 6 4664
process 7 8188
call back :  6012
<Future at 0x2d3e518 state=finished returned str>
**
call back :  6012
<Future at 0x2d3e470 state=finished returned str>
*
process 3 end
process 8 1756
call back :  6012
<Future at 0x2d3e5c0 state=finished returned str>
***
process 4 end
process 9 2356
call back :  6012
<Future at 0x2d3e668 state=finished returned str>
****
process 5 end
process 10 9636
call back :  6012
<Future at 0x2d3e748 state=finished returned str>
*****
process 6 end
process 11 4664
process 7 end
call back :  6012
process <Future at 0x2d3e828 state=finished returned str>
12 ******8188

process 8 end
call back :  6012
<Future at 0x2d3e908 state=finished returned str>
*******
process 13 1756
call back :  6012
<Future at 0x2d3e9e8 state=finished returned str>
********
process 9 end
call back :  6012
<Future at 0x2d3eac8 state=finished returned str>
*********
process 14 2356
process 10 end
call back :  6012
<Future at 0x2d3eba8 state=finished returned str>
**********
process 15 9636
process 11 end
process 16 4664
process 12 end
process 17 8188
call back :  6012
<Future at 0x2d3ec88 state=finished returned str>
***********
process 13 end
process 18 1756
call back :  6012
<Future at 0x2d3ed68 state=finished returned str>
************
call back :  6012
<Future at 0x2d3ee48 state=finished returned str>
*************
process 14 end
process 19 2356
call back :  6012
<Future at 0x2d3ef28 state=finished returned str>
**************
process 15 end
call back :  6012
<Future at 0x2d54048 state=finished returned str>
***************
process 16 endprocess 17 end

call back :  6012
<Future at 0x2d54128 state=finished returned str>
****************
call back :  6012
<Future at 0x2d54208 state=finished returned str>
*****************
process 18 end
call back :  6012
<Future at 0x2d542e8 state=finished returned str>
******************
process 19 end
call back :  6012
<Future at 0x2d543c8 state=finished returned str>
*******************
View Code
原文地址:https://www.cnblogs.com/lyj910313/p/10787418.html