Last active
August 29, 2015 14:20
-
-
Save porterjamesj/691613fabfc5b5932df6 to your computer and use it in GitHub Desktop.
flask request close insanity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from flask import Flask, Response, current_app | |
class Stream(object): | |
def __init__(self, string): | |
self.bytes = 0 | |
self.string = string | |
def __iter__(self): | |
while True: | |
self.bytes += len(self.string) | |
yield self.string | |
app = Flask(__name__) | |
app.foos = 0 | |
def make_callback(stream): | |
t = time.time() | |
foos = current_app.foos | |
def callback(): | |
print "closing!" | |
print "bytes streamed: {}".format(stream.bytes) | |
print t, foos | |
return callback | |
@app.route("/<where>") | |
def route(where): | |
if where == "foo": | |
current_app.foos += 1 | |
stream = Stream(where) | |
resp = Response(stream) | |
resp.call_on_close(make_callback(stream)) | |
return resp | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment