龙卷风文件上传被阻止

时间:2014-02-20 12:25:14

标签: python asynchronous tornado

我正在尝试使用Tornado创建文件上传服务。

但是我注意到当多个用户上传文件时,龙卷风等待第一个用户完成并处理下一个文件。

在Tornado上进行ASYNC文件上传的正确方法是什么?

我的帖子:

  @tornado.web.asynchronous
  def post(self):
    list_of_img = ['png','jpg','jpeg','gif']
    list_of_files = ['rtf','txt','ppt','pptx','doc','docx','pdf','xls','xlsx','rar','zip','tgz','bz','gz','tar','bz2','3gp','mp4','m15','avi','mp3']
    path  =  __UPLOADS__
    try:
      fileinfo = self.request.files['file'][0]
      filebody = fileinfo['body']
      filename = fileinfo['filename']
      filetype = fileinfo['content_type']
      extn     = os.path.splitext(filename)[1]

      n_filename = str(uuid.uuid4()) + extn

      # rcv file
      print "saving", n_filename + "..."
      output_file = open(n_filename, 'w')
      output_file.write(filebody)

      # upload to s3
      print "uploading", n_filename + "..."

      self.upload(path, n_filename)

      # clean up
      print "cleaning", n_filename + "..."
      self.delete_local(n_filename)

      self.finish(n_filename)
    except KeyError, key:
      delete = self.get_argument("delete", None, True)
      if delete:
        filename = self.get_argument("filename", None, True)

        print "deleting", filename + "..."
        self.delete(path, filename)

        self.finish(filename)

2 个答案:

答案 0 :(得分:1)

@asynchronous装饰器应该用于标记已经异步的方法;它不会使方法异步。这个post方法是同步的,因为它在将控制权返回给IOLoop之前完成了它要做的所有事情。您需要使upload()方法异步(这通常意味着它将采用回调参数或返回Future),然后在不阻塞post()的情况下调用它(我建议使用{ {1}}装饰器并通过让@gen.coroutine返回来调用慢速操作。)

答案 1 :(得分:0)

Gevent(Greenlets)是免费午餐的方式来解决BLocking部件。

from gevent import monkey
monkey.patch_all()

但要小心,有时会破坏事情。