Tornado的AsyncHTTPClient从1.2升级到2.0后不再工作

2024-05-13 03:26:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我决定今晚踢一下Tornado 2.0的轮胎,但它似乎为我在ASyncHTTPClient上做了很多工作。2.0版的发行说明中没有任何内容表明需要对如何使用ASyncHTTPClient进行任何实际更改:

[编辑:使代码更像一个明确的、自包含的示例]

import time
import threading
import functools

import tornado.ioloop
import tornado.web
from tornado.httpclient import *

class MainHandler(tornado.web.RequestHandler):

    def perform_task(self,finish_function):
      http_client = AsyncHTTPClient()
      tornado.ioloop.IOLoop.instance().add_callback(finish_function)
      # do something
      for i in range(0,10):
        print i
        time.sleep(1)
      request = tornado.httpclient.HTTPRequest("http://10.0.1.5:8888",method="POST",body="finished countdown")
      resp = http_client.fetch(request, self.handle_request)
      return

    def join_callback(self):
      # finish the request, also returns control back to ioloop's thread.
      self.finish()

    def handle_request(self, response):
      if response.error:
          print "Error:", response.error
      else:
          print response.body

    @tornado.web.asynchronous
    def get(self):
        self.write("Kicking off.")
        a_partial = functools.partial(self.perform_task,self.join_callback)
        self.thread = threading.Thread(target=a_partial)
        self.thread.start()
        self.write("\n<br/>Done in here, out of my hands now.")

    # just so this example has something to post to
    @tornado.web.asynchronous
    def post(self):
        self.write("POSTED: %s" % (self.request.body))

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

使用默认(非curl)ASyncHTTPClient时,我得到以下信息:

^{pr2}$

如果我加上: tornado.httpclient.AsyncHTTPClient。配置(“tornado.curl_httpclient.CurlAsyncHTTPClient“)

为了指定要使用PyCurl,我得到了以下异常:

   Traceback (most recent call last):
      File "/usr/lib/python2.6/threading.py", line 532, in __bootstrap_inner
        self.run()
      File "/usr/lib/python2.6/threading.py", line 484, in run
        self.__target(*self.__args, **self.__kwargs)
      File "migratotron.py", line 46, in perform_migration
        hc.fetch(request,success)
      File "/usr/local/lib/python2.6/dist-packages/tornado/curl_httpclient.py", line 81, in fetch
        self._process_queue()
      File "/usr/local/lib/python2.6/dist-packages/tornado/curl_httpclient.py", line 210, in _process_queue
        curl.info["headers"])
      File "/usr/local/lib/python2.6/dist-packages/tornado/curl_httpclient.py", line 276, in _curl_setup_request
        curl.setopt(pycurl.URL, request.url

)
TypeError: invalid arguments to setopt

我做的唯一不寻常的事情是调用这是我创建的另一个线程来做一些后台处理,它使用add_回调将控制权返回给ioloop线程。有人看过2.0版或类似的版本吗?在


Tags: inpyimportselfwebrequestlibusr
1条回答
网友
1楼 · 发布于 2024-05-13 03:26:59

这些错误消息有点神秘,但在这两种情况下,我认为它们是由于您的请求没有URL(除非callback是一个URL)。在

2.0将ASyncHTTPClient实现切换为SimpleHttpClient(来自curl_httpclient),但我认为给出的示例在Tornado1.2中也不起作用。在

相关问题 更多 >