python - 什么是 Python 等价于 C# 的 ContinueWith()
问题描述
这段代码的 Python 等价物是什么?
var firstTask = new Task(() => Foo());
var secondTask = firstTask.ContinueWith((fooResult) => Bar(fooResult));
firstTask.Start();
我假设它会使用 asyncio 库。我知道如何在 Python 中创建任务,但我找不到与 C# 的 ContinueWith() 执行相同操作的简单示例
解决方案
你可以使用Future.add_done_callback()
回调注册函数来做同样的事情;呼应 lambda 函数支持将是:
import asyncio
def continue_task_with(task, callback, *, loop=None):
"""When a task completes, schedule a new task.
The new task is created by calling the callback with the result of
the first task.
"""
def done_callback(fut):
asyncio.ensure_future(callback(fut), loop=loop)
task.add_done_callback(done_callback)
foo_task = asyncio.ensure_future(foo())
continue_task_with(foo_task, lambda result: bar(result))
asyncio.get_event_loop().run_forever()
所以这:
- 为
Foo()
- 注册一个回调,以便在任务完成时调用。
- 回调接收任务对象,并将该任务对象作为第一个参数传递给一个 lambda,该 lambda 从中创建一个
Bar()
协程。然后将 lambda 的结果安排为要运行的新任务。 - 然后调度
Foo()
任务;当该任务完成时,Bar()
回调被调用运行Bar()
。
演示:
>>> async def foo():
... print('Running foo')
... await asyncio.sleep(1)
... print('Completing foo')
... return 42
...
>>> async def bar(foo_task):
... print('Running bar')
... if not (foo_task.cancelled() or foo_task.exception()):
... print('Foo completed successfully, it received', foo_task.result())
... asyncio.get_event_loop().stop()
...
>>> foo_task = asyncio.ensure_future(foo())
>>> continue_task_with(foo_task, lambda result: bar(result))
>>> asyncio.get_event_loop().run_forever()
Running foo
Completing foo
Running bar
Foo completed successfully, it received 42
推荐阅读
- gradle - 如何在 gradle plugins 块中使用环境变量?
- php - Coinbase API PHP 获取当前资产 - 嵌套对象数组
- sql - 替换选择中的值
- node.js - 读取 kafka 主题并通过 Rest API 公开数据以供 prometheus 抓取(Nodejs)
- cytoscape.js - 当用户拖动并释放节点时,如何在 cytoscape 中捕获数据的位置?
- json - 为什么 rapidjson 有错误并且说 json 文件是 NULL 而它有数据?
- python - 我如何为熊猫结合 np.where 和 fillna
- c++ - 无法创建具有给定数量元素的链表
- html - 浮动按钮显示在网页下方
- powershell - Powershell 从安排好的报告中获取内容