-
-
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
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
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