4

I want to use flask to read a large data (essentially infinite) stream from a client. In the handler method for the URL, I want to read some data then yield, in an infinite loop. Will this work?

To clarify, I want do something as follows:

    @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.

Thank you in advance for your replies.

Ranga

  • your question is not clear may be you can check this http://www.html5rocks.com/en/tutorials/eventsource/basics/ you can yield the result as Response http://stackoverflow.com/questions/12232304/how-to-implement-server-push-in-flask-framework – Nava Jul 11 '14 at 17:47
  • @nava What is not clear in the question? Maybe the answer is not trivial, but the question seems well stated to me. – Jan Vlcinsky Jul 11 '14 at 17:49
  • @JanVlcinsky This question seems generic. what type of specific answer we can give? – Nava Jul 11 '14 at 17:53
  • @nava The answer I could imagine is sample or explanation, how to write an Flask app, which accepts chunked encoded data as described e.g. for [requests client](http://docs.python-requests.org/en/latest/user/advanced/#chunk-encoded-requests) and which allows consuming the data on server side without storing all the content in a file. – Jan Vlcinsky Jul 11 '14 at 17:58
  • @JanVlcinsky There I posted a relevant link in my first comment which might give some idea to you please check that. Thanks for clarifying the question – Nava Jul 11 '14 at 18:16
  • request.stream.read() in a loop? although I'm not sure that works infinitely – Tommi Komulainen Jul 11 '14 at 21:36

2 Answers2

0

I believe the right solution is to use websockets on the flask server rather than use raw HTTP post. Thank you all for your replies. I will update on how this goes.

0

Found the answer on this blog:

@app.route("/spectrumdb/stream",methods=["POST"])
def datastream():
    chunk_size = 4096
    while True:
        chunk = request.input_stream.read(chunk_size)
        if len(chunk) == 0:
            break
        // process chunk here
sigma1510
  • 1,165
  • 1
  • 11
  • 26