2014-07-11 49 views
2

我想用烧瓶从客户端读取大量数据(本质上是无限的)流。在URL的处理程序方法中,我想读取一些数据,然后在无限循环中生成。这会工作吗?烧瓶:如何读取流输入

为了澄清,我想要做一些事情如下:

@app.route("/spectrumdb/stream",methods=["POST"]) 
    def datastream(): 
     get the input stream of the request object. 
     Sit in an infinite loop, incrementally reading the input stream in chunks 
     and processing each chunk yeild the 
     interpreter after each chunk is processed. 

预先感谢您的答复。

朗高

+0

你的问题是不明确的可能是你可以检查这http://www.html5rocks.com/en/tutorials/eventsource/basics/你可以产生的结果作为响应http://stackoverflow.com/questions/12232304/how-to-implement-server-push-in-烧瓶架构 – Nava

+0

@nava问题中不清楚的是什么?也许答案不是微不足道的,但这个问题似乎对我很好。 –

+0

@JanVlcinsky这个问题似乎是通用的。我们可以给出什么类型的具体答案? – Nava

回答

0

我认为正确的解决方案是将烧瓶服务器上使用WebSockets,而不是使用原始的HTTP发布。谢谢大家的回复。我会更新这个过程。

-1

我会推荐阅读从瓶中的文档如下:http://flask.pocoo.org/docs/0.10/patterns/streaming/

基本用法:

@app.route('/large.csv') 
def generate_large_csv(): 
    def generate(): 
     for row in iter_all_rows(): 
      yield ','.join(row) + '\n' 
    return Response(generate(), mimetype='text/csv') 

你基本上使用生成函数生成大量的数据,并把它传递到响应对象。

如果你想在你的HTML文件中的数据渲染,你将最有可能使用JavaScript,这里是来自计算器内的优秀帖子:Streaming data with Python and Flask

+0

虽然这可能在理论上回答这个问题,[这将是可取的](/ meta.stackoverflow.com/q/8259)包括答案的重要部分在这里,并提供链接供参考。 –

+0

这是一个评论,而不是回答艾莫。 – Jaap