Skip to content

Instantly share code, notes, and snippets.

@8dspaces
Forked from sergray/async_flask.py
Created September 11, 2016 03:54
Show Gist options
  • Save 8dspaces/d5e7cdf2fc66c292742777d54acd6369 to your computer and use it in GitHub Desktop.
Save 8dspaces/d5e7cdf2fc66c292742777d54acd6369 to your computer and use it in GitHub Desktop.

Revisions

  1. @sergray sergray revised this gist Dec 8, 2014. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions async_flask.py
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,7 @@
    # need to patch sockets to make requests async
    monkey.patch_all()

    # TODO experiment with different chunk sizes
    CHUNK_SIZE = 1024 # bytes
    CHUNK_SIZE = 1024*1024 # bytes

    app = Flask(__name__) # pylint: disable=invalid-name
    app.debug = True
  2. @sergray sergray revised this gist Dec 8, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions async_flask.py
    Original file line number Diff line number Diff line change
    @@ -34,6 +34,7 @@ def seattle(requests_counter=[0]): # pylint: disable=dangerous-default-value

    def generator():
    "streaming generator logging the end of request processing"
    yield '' # to make greenlet switch
    for data in rsp.iter_content(CHUNK_SIZE):
    yield data
    app.logger.debug('finished %d', request_num)
  3. @sergray sergray created this gist Dec 7, 2014.
    53 changes: 53 additions & 0 deletions async_flask.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    """Asynchronous requests in Flask with gevent"""

    from time import time

    from flask import Flask, Response

    from gevent.pywsgi import WSGIServer
    from gevent import monkey

    import requests

    # need to patch sockets to make requests async
    monkey.patch_all()

    # TODO experiment with different chunk sizes
    CHUNK_SIZE = 1024 # bytes

    app = Flask(__name__) # pylint: disable=invalid-name
    app.debug = True


    @app.route('/Seattle.jpg')
    def seattle(requests_counter=[0]): # pylint: disable=dangerous-default-value
    """Asynchronous non-blocking streaming of relatively large (14.5MB) JPG
    of Seattle from wikimedia commons.
    """
    requests_counter[0] += 1
    request_num = requests_counter[0]
    url = 'http://upload.wikimedia.org/wikipedia/commons/3/39/Seattle_3.jpg'

    app.logger.debug('started %d', request_num)

    rsp = requests.get(url, stream=True)

    def generator():
    "streaming generator logging the end of request processing"
    for data in rsp.iter_content(CHUNK_SIZE):
    yield data
    app.logger.debug('finished %d', request_num)

    return Response(generator(), mimetype='image/jpeg')


    def main():
    "Start gevent WSGI server"
    # use gevent WSGI server instead of the Flask
    http = WSGIServer(('', 5000), app.wsgi_app)
    # TODO gracefully handle shutdown
    http.serve_forever()


    if __name__ == '__main__':
    main()