Python上的非阻塞生成器 [英] Non-blocking generator on Python
问题描述
我正在使用QT应用程序中的请求模块中的生成器功能,与请求流示例中的几乎相同:
I'm using a generator function from the requests module in a QT-Application, pretty much the same as in the requests-streaming example:
import json
import requests
def get_stream():
r = requests.get('http://httpbin.org/stream/20', stream=True)
for line in r.iter_lines():
if line:
yield json.loads(line)
def consume_stream():
for message in get_stream():
#do something
但是,当没有传入响应(例如来自Twitters Streaming API的不规则传入推文)时,生成器get_stream
将阻止consume_stream
方法.
However, when there is no incoming response (f.e. irregularly incoming tweets from Twitters Streaming API), the generator get_stream
will block the consume_stream
method.
在生成器没有立即产生但必须等待传入消息等从而阻塞使用者的任何情况下,都可能发生这种情况.
This might occur in any situation where a generator does not yield immediately, but hast to wait for incoming messages etc., and therefore blocks the consumer.
Python中是否有任何模式可以让您以非阻塞方式使用生成器,即,如果生成器屈服,则处理其结果,否则执行其他操作直到出现下一个结果?
Is there any pattern in Python where you can consume a generator in a non-blocking way, i.e. if the generator yields, process it's results, otherwise do something else until the next results are occuring?
推荐答案
看看 Queue
在python中实现.
Take a look at the producer-consumer pattern. It is commonly implemented in python using a Queue
.
通常在线程或其他进程(Queue
支持其中之一)中运行的生产者,只是将消息放入队列中.消费者只要有感觉,就会从队列中弹出消息.此操作支持timeout
参数.
The producer, typically running in a thread or another process (Queue
supports either), simply puts messages in the queue. The consumer, whenever it feels like it, pops messages from the queue. This operation supports a timeout
argument.
这篇关于Python上的非阻塞生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!