HTTPClient 使用例子:
    from tornado.httpclient import HTTPClient 

    def synchronous_fetch(url): 
     http_client = HTTPClient()
     response = http_client.fetch(url)
     return response.body
AsyncHTTPClient使用例子:

方法1:
from tornado.httpclient import AsyncHTTPClient def asynchronous_fetch(url, callback):
  http_client = AsyncHTTPClient()
def handle_response(response): # 创建一个函数内的函数,来处理返回的结果
callback(response.body)
http_client.fetch(url, callback=handle_response) # 异步处理结束后会调用指定的callback的函数 方法2:
from tornado.httpclient import AsyncHTTPClient
from tornado.concurrent import Future def async_fetch_future(url):
http_client = AsyncHTTPClient()
my_future = Future()
fetch_future = http_client.fetch(url)
fetch_future.add_done_callback(lambda f: my_future.set_result(f.result()))
return my_future
方法3:
from tornado.httpclient import AsyncHTTPClient
from tornado import gen @gen.coroutine #添加异步访问的装饰器
def fetch_coroutine(url):
http_client = AsyncHTTPClient()
response = yield http_client.fetch(url) # 获取异步结果时要使用yield
raise gen.Return(response.body) # 使用抛出异常的方式来返回结果,不能使用return来返回 以下知识是额外的可以了解下,但不保证知识是完整的:
  async and await 在python3.5,tornado4.3中可以了解下   例子:
async deffetch_coroutine(url):
   http_client = AsyncHTTPClient()
response = await http_client.fetch(url)
return response.body
使用yield来遍历异步的结果是,以下方法是在项目中没有试验过的
--------------------------------- start --------------------------------------
@gen.coroutine
def parallel_fetch(url1, url2):
resp1, resp2 = yield [http_client.fetch(url1),
http_client.fetch(url2)] @gen.coroutine
def parallel_fetch_many(urls):
responses = yield [http_client.fetch(url) for url in urls]
# responses is a list of HTTPResponses in the same order @gen.coroutine
def parallel_fetch_dict(urls):
responses = yield {url: http_client.fetch(url)
for url in urls}
# responses is a dict {url: HTTPResponse} --------------------------------- end ----------------------------------------
												

Python下HttpHTTPClient和AsyncHTTPClient的更多相关文章

  1. python下ssh的简单实现

    python下的ssh都需要借助第三方模块paramiko来实现,在使用前需要手动安装. 一.python实现ssh (1) linux下的ssh登录 root@ubuntu:~# ssh morra ...

  2. python下编译py成pyc和pyo

     python下编译py成pyc和pyo   其实很简单, 用 python -m py_compile file.py python -m py_compile /root/src/{file1,f ...

  3. Python下划线与命名规范

    Python下划线与命名规范 先看结论,节省只想知道答案你的宝贵时间: _xxx 不能用于from module import * 以单下划线开头的表示的是protected类型的变量.即保护类型只能 ...

  4. python下的orm基本操作(1)--Mysql下的CRUD简单操作(含源码DEMO)

    最近逐渐打算将工作的环境转移到ubuntu下,突然发现对于我来说,这ubuntu对于我这种上上网,收收邮件,写写博客,写写程序的时实在是太合适了,除了刚接触的时候会不怎么完全适应命令行及各种权限管理, ...

  5. Python下科学计算包numpy和SciPy的安装

    转载自:http://blog.sina.com.cn/s/blog_62dfdc740101aoo6.html Python下大多数工具包的安装都很简单,只需要执行 “python setup.py ...

  6. python下的复杂网络编程包networkx的安装及使用

    由于py3.x与工具包的兼容问题,这里采用py2.7 1.python下的复杂网络编程包networkx的使用: http://blog.sina.com.cn/s/blog_720448d30101 ...

  7. Python学习入门基础教程(learning Python)--5.1 Python下文件处理基本过程

    Python下的文件读写操作过程和其他高级语言如C语言的操作过程基本一致,都要经历以下几个基本过程. 1. 打开文件 首先是要打开文件,打开文件的主要目的是为了建立程序和文件之间的联系.按程序访问文件 ...

  8. python下读取excel文件

    项目中要用到这个,所以记录一下. python下读取excel文件方法多种,用的是普通的xlrd插件,因为它各种版本的excel文件都可读. 首先在https://pypi.python.org/py ...

  9. python下异常处理

    1.python下异常如何处理: #encoding=utf-8 """ python遇到异常,程序直接运行 try: "判断有可能抛出异常的代码" ...

随机推荐

  1. 修改了jdk在环境变量中的路径怎么cmd中的jdk版本没有变

    把path路径下的jdk配置放在前面%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot ...

  2. Error: Could not find gradle wrapper within Android SDK. Might need to update your Android SDK. Looked here: C:\Users\Administrator\AppData\Local\Android\sdk\tools\templates\gradle\wrapper

    在Windows7上运行 “cordova build Android” 报错,如下: C:\test\hello> cordova build androidANDROID_HOME=C:\U ...

  3. Nginx 配置为https服务器

    本机作为https服务器 server { listen ssl; server_name localhost; ssl_certificate ssl/server.crt; ssl_certifi ...

  4. Doris与Hadoop yarn混合部署遇到的坑

    Doris默认端口 Yarn 默认端口: 如图,端口冲突,在混合部署的情况下,会出现2个问题: 1. Yarn ResourceManager启动不起来 解决办法:修改yarn.resourceman ...

  5. The compiler compliance specified is 1.5 but a JRE 1.8 is used

    错误信息: The compiler compliance specified is 1.5 but a JRE 1.8 is used 解决办法: 右击项目>选择Properties 选择Ja ...

  6. [python] 初学python,级联菜单输出

    #Author:shijt china_map = { "河北": { '石家庄': ['辛集', '正定', '晋州'], '邯郸': ['涉县', '魏县', '磁县'], ' ...

  7. String,StringBuilder,StringBuffer三者的区别(Java)

    这三个类之间的区别主要是在两个方面,即运行速度和线程安全这两方面. 1. 首先说运行速度,或者说是执行速度,在这方面运行速度快慢为:StringBuilder > StringBuffer &g ...

  8. QT_QSlider的总结

    当鼠标选中QSlider 上时,通过点击的数值为setpageStep():通过左右方向键按钮移动的数值为setsingleStep(). 鼠标滚轮上面两者都不行,不知道是什么原因! 应用: http ...

  9. spring事务传播实现源码分析

    转载. https://blog.csdn.net/qpfjalzm123/article/details/83717367 本文只是对spring事务传播实现的流程进行简单的分析,如有不对之处请指出 ...

  10. TIDB资料收集

    https://github.com/pingcap/docs-cn https://github.com/pingcap/docs-cn/blob/master/op-guide/binary-de ...