Skip to content

Instantly share code, notes, and snippets.

@porterjamesj
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save porterjamesj/691613fabfc5b5932df6 to your computer and use it in GitHub Desktop.
Save porterjamesj/691613fabfc5b5932df6 to your computer and use it in GitHub Desktop.
flask request close insanity
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