【问题标题】:tornadoweb upload to another server blocking processtornadoweb 上传到另一个服务器阻塞进程
【发布时间】:2016-03-30 20:49:42
【问题描述】:

我创建了从 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}))

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

【问题讨论】:

    标签: upload tornado multipart


    【解决方案1】:

    “请求”库不是为 Tornado 构建的;它不是异步的。改用 Tornado 自己的 AsyncHTTPClient:

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

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

    【讨论】:

    • 我用 open(fileurl, 'r') as f: body = f.read() response = yield httpclient.AsyncHTTPClient().fetch('192.168.174.129:8080/…{"Content-Type" : "text/plain"},body={"myFile":body}) 但我得到了错误 Exception: Expected bytes, unicode, or None;得到
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多