【发布时间】:2020-01-02 07:30:50
【问题描述】:
我有一个协程,我想在 Jupyter 笔记本中作为“后台作业”运行它。我见过ways to accomplish this using threading,但我想知道是否也可以挂钩到笔记本的事件循环中。
例如,假设我有以下课程:
import asyncio
class Counter:
def __init__(self):
self.counter = 0
async def run(self):
while True:
self.counter += 1
await asyncio.sleep(1.0)
t = Counter()
我想执行 run 方法(无限循环),同时仍然能够随时检查 t.counter 变量。有什么想法吗?
【问题讨论】:
标签: python jupyter python-asyncio jupyter-notebook