【发布时间】:2015-02-21 00:22:01
【问题描述】:
我希望有一个协程连续接受数据并使其以固定间隔产生速度(例如:就每分钟接收的项目而言)
@coroutine
def speed(target=None):
messages = Queue()
def calculate():
while True:
time.sleep(1)
messages.put(1/count)
Thread(target=calculate).start()
while True:
item = (yield)
count += 1
... how do I receive messages in a nonblocking fashion?
我试图弄清楚我是否需要启动一个线程并使用一个队列从该队列中产生,或者以其他方式来执行此并行计算(速度、吞吐量、平均值、以固定间隔发出最后一个值、等)
【问题讨论】:
-
这是真正的 Python 代码吗?
-
是的,不是完整的程序,而是真实的代码。
coroutine装饰器来自@dabeaz -
好吧,我假设您在第二行缺少
def,而在第 9 行,t可能应该大写。
标签: python multithreading yield