• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
菩提叶子
博客园    首页    新随笔    联系   管理    订阅  订阅
python 回调函数

回调函数: 回头调用一下
把函数当成一个参数传递给另外一个函数
在当前函数执行完毕之后,最后调用一下当参数传递进来的函数
add_done_callback(回调函数)

功能:
支付状态:
退款状态:
转账的状态
把想要的相关的成员信息写在回调函数之后,
通过支付接口调用之后,后台会自动把想要的数据加载到回调函数中
从而看到最后的状态.

from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
from threading import current_thread  as cthread
import os,time
def func1(i):
    print("process start ... " , os.getpid())
    time.sleep(1)
    print("process end ... ", i)
    return "*" * i

def func2(i):
    print("thread start ... " , cthread().ident)
    time.sleep(1)
    print("thread end ... ", i)
    return "*" * i

def call_back1(obj):
    print("<===回调函数callback进程号===>" , os.getpid())
    print(obj.result())
    
def call_back2(obj):
    print("<===回调函数callback线程号===>" ,cthread().ident)
    print(obj.result())

# (1) 进程池的回调函数: 由主进程执行调用完成的
"""
if __name__ == "__main__":
    p = ProcessPoolExecutor()
    for i in range(1,11):
        res = p.submit(func1,i)
        # print(res.result())
        res.add_done_callback(call_back1)
        # self.func(func2)
    p.shutdown()
    print("主进程执行结束 ... " , os.getpid())
"""

# (2) 线程池的回调函数 : 由当前子线程调用完成的
if __name__ == "__main__":
    tp = ThreadPoolExecutor(5)
    for i in range(1,11):
        res = tp.submit(func2,i)
        res.add_done_callback(call_back2)
        
    tp.shutdown()
    print("主线程执行结束 ... " , cthread().ident)
    

 

posted on 2022-10-26 18:41  菩提叶子  阅读(1440)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3