Created
October 7, 2012 19:06
-
-
Save peterbe/3849257 to your computer and use it in GitHub Desktop.
Async creation of thumbnails with RQ
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
@route(r'/thumbnails/(?P<image>\w{1}/\w{2}/\w{6})/(?P<width>\w{1,3})' | |
r'.(?P<extension>png|jpg)', | |
name='thumbail') | |
class TileHandler(BaseHandler): | |
@tornado.web.asynchronous | |
@tornado.gen.engine | |
def get(self, image, width, extension): | |
width = int(width) | |
# stick it on a queue | |
q = Queue(connection=self.application.redis_connection) | |
job = q.enqueue( | |
make_thumbnail, | |
image, | |
width, | |
'png', | |
self.application.settings['static_path'] | |
) | |
ioloop_instance = tornado.ioloop.IOLoop.instance() | |
while True: | |
yield tornado.gen.Task( | |
ioloop_instance.add_timeout, | |
time.time() + 1 | |
) | |
if job.result is not None: | |
thumbnail_filepath = job.result | |
break | |
self.set_header('Content-Type', 'image/png') | |
self.write(open(thumbnail_filepath, 'rb').read()) | |
self.finish() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment