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. Mysql 之闪回技术 binlog2sql

    1.下载 https://github.com/danfengcao/binlog2sql http://rpmfind.net Search: python-pip pip 是一个Python包管理 ...

  2. a++ 与 ++a 的运算

    var a=5: b=a++和b=++a的区别: 前者是先赋值,再自加,即b=a:a=a+1: //结果b=5,a=6 后者是先自加,再赋值,即a=a+1;b=a;  //结果a=6,b=6

  3. 关于jQuery点击事件叠加问题

    先来看个例子: html: <body> <button id="btn">按钮</button> <button id="bt ...

  4. Struts2 --简单留言系统

    1.创建数据表,与servlet中相同 2.创建web项目,添加struts2模块,url 选 /*,web.xml中会自动注册struts2,同时src下会自动生成struts2配置文件struts ...

  5. Django-models的字段类型

    model的field类型 1.models.AutoField   ---自增列 = int(11)    如果没有的话,默认会生成一个名称为 id 的列,如果要显示的自定义一个自增列,必须将给列设 ...

  6. useful tools and website

    1.https://www.processon.com/   在线流程图制作网站 2.http://www.easyicon.net/  专门下载图标的网站 3.https://www.lfd.uci ...

  7. [SHOI2012]信用卡凸包(计算几何)

    /* 考验观察法?? 可以发现最终答案等于所有作为圆心的点求出凸包的周长加上一个圆的周长 向量旋转 (x1, y1) 相较于 (x2, y2) 旋转角c 答案是 (dtx * cosc - dty * ...

  8. Python列表生成式和生成器

    [1]列表生成器:列表生成式就是一个用来生成列表的特定语法形式的表达式. 1.基础语句结构:[exp for iter_var in iterable例如:a=[f(x) for x in range ...

  9. lunix 项目部署 *****

    linux基本管理命令 服务器上安装服务,python3.6(宿主机上的物理解释器)1.虚拟解释器virtualenv虚拟出多个干净.隔离的python解释器环境问题:管理上较为麻烦,需要找到venv ...

  10. MyBatis批量增删改查操作

      前文我们介绍了MyBatis基本的增删该查操作,本文介绍批量的增删改查操作.前文地址:http://blog.csdn.net/mahoking/article/details/43673741 ...