Skip to content

Instantly share code, notes, and snippets.

@fabiant7t
Created March 22, 2018 17:19
Show Gist options
  • Save fabiant7t/7f6c141e23abd34cfd828339e9e060ed to your computer and use it in GitHub Desktop.
Save fabiant7t/7f6c141e23abd34cfd828339e9e060ed to your computer and use it in GitHub Desktop.
Simple test that proxies remote content (and shows a memory issue in Tornado)
"""
Simple test that proxies remote content (and shows a memory issue in Tornado)
Call http://localhost:8888/200 and you will see that the request object dies.
But call http://localhost:8888/400 and the request object stays in memory forever (or until you press <Ctrl+c>
"""
#!/usr/bin/python3
import weakref
import tornado.httpclient
import tornado.ioloop
import tornado.gen
import tornado.web
class MainHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def get(self, status):
request = tornado.httpclient.HTTPRequest(
url='https://httpstat.us/{}'.format(status)
)
weakref.finalize(request, lambda: print('Request died'))
client = tornado.httpclient.AsyncHTTPClient()
response = yield client.fetch(request, raise_error=False)
# client.close()
self.set_status(response.code)
self.write('Status %d' % response.code)
self.finish()
def make_app():
return tornado.web.Application([
(r"/(?P<status>\d{3})", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment