tornadoweb上传到另一个服务器阻止进程

时间:2016-03-30 04:33:33

标签: upload tornado multipart

我从tornadoweb到另一台服务器创建了上传器。它会阻止进程,所以我必须等到所有上传过程完成才能访问另一个url(ShowQueue类) 这是我的代码

    def create_callback(encoder,fileurl):
        encoder_len = encoder.len
        queueupload[fileurl] = {}
        queueupload[fileurl]["size"] = encoder_len

        def callback(monitor):
            queueupload[fileurl]["current"] = monitor.bytes_read
            # print queueupload
            # bar.show(monitor.bytes_read)
        return callback

class ShowQueue(tornado.web.RequestHandler):
    @gen.coroutine
    def get(self):
        self.write(tornado.escape.json_encode({'success':True,'queuelist':queueupload}))

class UploadFile(tornado.web.RequestHandler):
    @gen.coroutine
    def get(self):
        fileurl=self.get_argument("fileurl", None)
        taxo=self.get_argument("taxonomi", None)
        try:
            filename = fileurl.split("\\")[-1]
            # print filename
            encoder = MultipartEncoder({
                'taxonomi': taxo,
                'myFile': (filename, open(fileurl, 'rb'), 'text/plain'),
                })

            callback = create_callback(encoder,fileurl)
            monitor = MultipartEncoderMonitor(encoder, callback)
            if(taxo!=""):
                arrtaxo = taxo.split("/")
                taxo = ":".join(arrtaxo)
                taxo = "/"+taxo
            r = requests.post('http://192.168.123.123:8080/upload'+taxo, data=monitor,
                              headers={'Content-Type': monitor.content_type})
            if fileurl in queueupload:
                del queueupload[fileurl]
            self.write(tornado.escape.json_encode({'success':True,'reason':r.json()}))
        except Exception as e:
            exc = 'Exception: %s %s' % (e, filename)
            self.set_status(500)
            self.write(json_encode({'success':False,'exception':exc}))

如何在不阻止所有进程的情况下异步创建上传器工作并仍然可以取消控制(我使用回调来取消进程但尚未实现)?

1 个答案:

答案 0 :(得分:0)

"要求"图书馆不是为龙卷风建造的;它不是异步的。请使用Tornado自己的AsyncHTTPClient:

http://www.tornadoweb.org/en/latest/httpclient.html#tornado.httpclient.AsyncHTTPClient

http_client = AsyncHTTPClient()
r = yield http_client.fetch(url, method="post", ...)