【发布时间】:2015-08-17 10:51:01
【问题描述】:
我是龙卷风的新手。
我想要的是编写一些函数来异步获取网页。由于这里不涉及请求处理程序、应用程序或服务器,我想我可以单独使用 tornado.httpclient.AsyncHTTPClient。 但是所有示例代码似乎都在龙卷风服务器或请求处理程序中。当我尝试单独使用它时,它永远不会起作用。 例如:
def handle(self,response):
print response
print response.body
@tornado.web.asynchronous
def fetch(self,url):
client=tornado.httpclient.AsyncHTTPClient()
client.fetch(url,self.handle)
fetch('http://www.baidu.com')
它说“'str'对象没有属性'application'”,但我想单独使用它?
或:
@tornado.gen.coroutine
def fetch_with_coroutine(url):
client=tornado.httpclient.AsyncHTTPClient()
response=yield http_client.fetch(url)
print response
print response.body
raise gen.Return(response.body)
fetch_with_coroutine('http://www.baidu.com')
也不行。
之前,我尝试将回调传递给 AsyncHTTPHandler.fetch,然后启动 IOLoop,它可以工作并打印网页源代码。但我不知道如何处理 ioloop。
【问题讨论】:
标签: python asynchronous tornado