繁体   English   中英

龙卷风文件上传被阻止

[英]tornado file upload is blocking

我正在尝试使用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)

@asynchronous装饰器应用于标记已经异步的方法。 它不会使方法异步。 post方法是同步的,因为它在将控制返回给IOLoop之前已完成了将要执行的所有操作。 您需要使upload()方法异步(这通常意味着它将采用回调参数或返回Future ),然后调用它而不会阻塞post() (我建议使用@gen.coroutine装饰器并调用慢速操作通过产生他们返回的Futures )。

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

from gevent import monkey
monkey.patch_all()

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM