我通过Python for.Net使用Python 3.6中的第三方.Net库,它使用事件处理程序向我的应用程序提供异步数据,类似于下面的玩具示例:
import clr
from System import Timers
def tock(__, args):
print(args.SignalTime)
timer = Timers.Timer()
timer.Interval = 1000
timer.Elapsed += tock
timer.Enabled = True
while True:
pass
我想把这些数据输入异步生成器,比如:
^{pr2}$显然,仅仅在事件处理程序函数上抛出asyc
和yield
,并对计时器进行await
,显然不能做到这一点,但是有没有一种简单的方法来实现这一点呢?在
由于“异步”数据来自另一个线程,因此您需要在asyncio和调用
tock
的线程之间建立一个桥。在异步端,您将需要实现一个异步生成器,并使用async for
循环对其进行迭代。例如(未测试):相关问题 更多 >
编程相关推荐