Skip to content

Instantly share code, notes, and snippets.

@peterbe
Created October 7, 2012 19:06
Show Gist options
  • Save peterbe/3849257 to your computer and use it in GitHub Desktop.
Save peterbe/3849257 to your computer and use it in GitHub Desktop.
Async creation of thumbnails with RQ
@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