Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@zhyq0826
Forked from davidbirdsong/Future_illustrate
Last active August 29, 2015 14:27
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 zhyq0826/b62fabdf73ca83da6c91 to your computer and use it in GitHub Desktop.
Save zhyq0826/b62fabdf73ca83da6c91 to your computer and use it in GitHub Desktop.
use ioloop.IOLoop.instance().add_future to create a callback for a completed Future object
from concurrent.futures import ThreadPoolExecutor
from tornado.concurrent import run_on_executor, Future
from tornado import httpclient,ioloop,web,gen
import request
import sys
httpclient.AsyncHTTPClient.configure('tornado.curl_httpclient.CurlAsyncHTTPClient', max_clients=10)
# TODO: remove
class Foo(Exception): pass
def dot():
"""callback for showing that IOLoop is still responsive while we wait"""
sys.stdout.write('.')
sys.stdout.flush()
num_exec = 10
EXECUTOR = ThreadPoolExecutor(max_workers=num_exec)
class HwyManHandler(web.RequestHandler):
def initialize(self, **kwargs):
self.backend_manager = kwargs.get('backend_manager')
self.executor = EXECUTOR
@web.asynchronous
@gen.coroutine
def get(self):
img = request.ImgixSourceImg(self.request, self.backend_manager)
client = httpclient.AsyncHTTPClient()
url, config = img.get_read_config()
req = httpclient.HTTPRequest(url, **config)
rep = yield client.fetch(req)
if rep.code != 200:
raise Foo, '%s\n%s' % (req.reason, req.body)
# url, config = img.get_write_config(rep)
# req = httpclient.HTTPRequest(url, **config)
# rep = yield client.fetch(req)
self.add_header('X-Iccel-Redirect', img.fname)
self.set_status(rep.code)
ioloop.IOLoop.instance().add_future(self.flush_to_store(img), test)
self.finish()
@run_on_executor
def flush_to_store(self, imgix_source):
import time
time.sleep(2)
print 'slept!!'
return imgix_source.flush()
def test(f):
import traceback
print 'in cb'
e = f.exception()
if e is not None:
print e
print 'getting result'
print f.result()
def main():
trackers = ['boo!']
application = web.Application([
(r'http.+', HwyManHandler),
(r'/.+$', HwyManHandler,
dict(backend_manager=request.ImgixSourceBackends(trackers, num_exec))),
], debug=True)
beat = ioloop.PeriodicCallback(dot, 3000)
beat.start()
application.listen(8888)
try:
ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
# TODO: figure out how to drain the ioloop
# and release the socket
print ' Interrupted'
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment