tornado关于AsyncHTTPClient的使用笔记

先来一段同步的httpclient使用代码

url = 'https://www.baidu.com/'

http_client = HTTPClient()
response = http_client.fetch(url)
print("xu jun test" + str(response.body))

 

然后是异步调用AsyncHTTPClient:

url = 'https://www.baidu.com/'


async def test():
    http_client = AsyncHTTPClient()
    result = await http_client.fetch(url)
    print(result.body)


if __name__ == '__main__':
    io_loop = ioloop.IOLoop.current()
    io_loop.run_sync(test)

还有一种是通过回调函数的方式使用,注意如果没反应,要在协程中使用

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

url = 'https://www.baidu.com/'

http_client = AsyncHTTPClient()
http_client.fetch(url, handle_request)

 

PS.由于tornado的版本升级较快,网上很多文档和API都过时了.所有有时候直接看源代码,或者找网上的示例代码,会更有效!

转载于:https://www.cnblogs.com/xujanus/p/8605554.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值