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

2 投票
1 回答
4317 浏览
提问于 2025-04-16 20:29

我决定今晚试试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时,我得到了以下结果:

Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/tornado/simple_httpclient.py", line 259, in cleanup
    yield
  File "/usr/local/lib/python2.6/dist-packages/tornado/simple_httpclient.py", line 186, in __init__
    functools.partial(self._on_connect, parsed))
  File "/usr/local/lib/python2.6/dist-packages/tornado/iostream.py", line 120, in connect
    self.socket.connect(address)
AttributeError: 'NoneType' object has no attribute 'connect'

如果我加上:

tornado.httpclient.AsyncHTTPClient.configure("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_callback将控制权返回给ioloop线程。有没有人遇到过2.0或类似的情况?

1 个回答

2

这些错误信息有点难懂,但我觉得在这两种情况下,问题都是因为你的请求没有提供一个网址(除非callback是一个网址)。

2.0版本把ASyncHTTPClient的实现换成了simple_httpclient(之前是curl_httpclient),不过我觉得在Tornado 1.2版本中,给出的例子也不太可能有效。

撰写回答