I have a handler that, among other things, resizes an uploaded image
file by piping the file data through GraphicsMagick using
subprocess.Popen. Here's a simplified version:
#!/usr/bin/env python
import subprocess
import tornado.httpserver
import tornado.web
class ImageHandler(tornado.web.RequestHandler):
def get(self):
self.write('<form method="post" enctype="multipart/form-
data">'
'<input type="file" name="image"/>'
'<input type="submit" value="Convert Image"/>'
'</form>')
def post(self):
command = "gm convert - -resize 50 png:-"
pipe = subprocess.Popen(command.split(),
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
image = self.request.files["image"][0]["body"]
image = pipe.communicate(image)[0]
self.set_header("Content-Type", "image/png")
self.write(image)
def main():
app = tornado.web.Application([(r"/", ImageHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(8080)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
I would like to do this asynchronously, so that the imaging processing
does not block, but instead calls a callback when complete.
I found this asynchronous implementation of subprocess.Popen:
PEP:
http://www.python.org/dev/peps/pep-3145/
Code:
http://code.google.com/p/subprocdev/source/browse/subprocess.py
However, it seems like this could be accomplished with the tornado
ioloop, but I'm still wrapping my head around it and epoll.
Any suggestions?
Thanks in advance,
Neil