分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

tornado关于AsyncHTTPClient的使用笔记

发布时间:2023-09-06 01:46责任编辑:白小东关键词:暂无标签

先来一段同步的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都过时了.所有有时候直接看源代码,或者找网上的示例代码,会更有效!

tornado关于AsyncHTTPClient的使用笔记

原文地址:https://www.cnblogs.com/xujanus/p/8605554.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved