def apply_async(func, args, callback):
# Compute the result
result = func(*args)
# Invoke the callback with the result
callback(result)
def print_result(result):
print('Got:', result)
def add(x, y):
return x + y
apply_async(add, (2, 3), callback=print_result)
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/cookbook/a22.py
('Got:', 5)
# !/usr/bin/env python
# -*- coding: utf-8 -*-
def apply_async(func, args, callback):
# Compute the result
result = func(*args)
# Invoke the callback with the result
callback(result)
def print_result(result):
print('Got:', result)
def add(x, y):
return x + y
apply_async(add, (2, 3), callback=print_result)
class ResultHandler:
def __init__(self):
self.sequence = 0
def handler(self, result):
self.sequence += 1
print ('[{}] Got: {}'.format(self.sequence, result))
##实例化对象
r = ResultHandler()
##r.handler 调用对象方法
apply_async(add, (2, 3), r.handler)
C:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/mycompany/cookbook/a22.py
('Got:', 5)
[1] Got: 5
python 回调函数
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
下一篇:python的装饰器 就是闭包
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java实现异步回调返回给前端
java实现异步回调返回给前端
Java Async 数据