【发布时间】:2015-10-30 12:15:19
【问题描述】:
我在使用 Python Stompest 库在帧到达时生成帧时遇到问题。当我在函数中包含 yield 关键字时,代码不会像它应该那样阻塞并等待帧。
def receive(stomp):
received_messages = 0
while True:
frame = stomp.receiveFrame()
stomp.ack(frame)
yield frame.body
但是,如果我所做的只是打印框架主体,那么它会像它应该的那样等待
def receive(stomp):
while True:
frame = stomp.receiveFrame()
stomp.ack(frame)
print frame.body
在这种情况下使用yield 有什么问题吗?
【问题讨论】:
-
你怎么打电话给
receive?如果你只是在做x = receive(whatever)就是这样,那么不会发生阻塞,因为生成器函数什么都不做,除非你对它们进行迭代或重复调用next。试试for frame in receive(whatever): do_something_here() -
你能创建一个MCVE吗?
标签: python python-2.7 yield