首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 回调装饰器

python 回调装饰器

作者头像
用户5760343
发布2019-10-21 11:34:21
1.1K0
发布2019-10-21 11:34:21
举报
文章被收录于专栏:sktjsktj

通过使用生成器和协程可以使得回调函数内联在某个函数中。 为了演示说明,假设你有如下所示的一个执行某种计算任务然后调用一个回调函数的函数(参考7.10小节):

def apply_async(func, args, , callback): # Compute the result result = func(args)

# Invoke the callback with the result
callback(result)

接下来让我们看一下下面的代码,它包含了一个 Async 类和一个 inlined_async 装饰器:

from queue import Queue from functools import wraps

class Async: def init(self, func, args): self.func = func self.args = args

def inlined_async(func): @wraps(func) def wrapper(args): f = func(args) result_queue = Queue() result_queue.put(None) while True: result = result_queue.get() try: a = f.send(result) apply_async(a.func, a.args, callback=result_queue.put) except StopIteration: break return wrapper 这两个代码片段允许你使用 yield 语句内联回调步骤。比如:

def add(x, y): return x + y

@inlined_async def test(): r = yield Async(add, (2, 3)) print(r) r = yield Async(add, ('hello', 'world')) print(r) for n in range(10): r = yield Async(add, (n, n)) print(r) print('Goodbye') 如果你调用 test() ,你会得到类似如下的输出:

5 helloworld 0 2 4 6 8 10 12 14 16 18 Goodbye 你会发现,除了那个特别的装饰器和 yield 语句外,其他地方并没有出现任何的回调函数(其实是在后台定义的)。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.10.18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档