Python下HttpHTTPClient和AsyncHTTPClient

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 ----------------------------------------
				
时间: 2024-08-26 23:26:16

Python下HttpHTTPClient和AsyncHTTPClient的相关文章

python 下的crc16计算模块 XCRC16

又一次突然遇到用python处理modbus通信而需要crc16校验的问题,当时在百度上没找到,在google上找到了一个外国人开发的python包,结果安装好了之后发现校验的不正确(可能是使用的模式串不一样,xcrc16的模式串为0xa001),后来事情过去了就写了一个包弥补一下,xcrc16 的意思是 extend crc->xcrc ,也是我的第一个开源项目,如果大家使用程序遇到什么情况也麻烦通知我下,我会第一时间进行维护. 介绍: xcrc16 模块是为了解决crc16校验问题而写 目前

在python下学习libsvm

1.下载libsvm,python,gnuplot(链接网上全有,压缩包自己保留着) 2.在python上的实现(主要用截图的形式展现) (1)输入命令寻求最优参数 (2) 参数c,g输出结果 gnuplot输出图像 (3)最后输入训练数据,训练数据,通过建立模型进行预测 大概也就这样了,grid.py里面需要改下gnuplot的路径 在python下学习libsvm,布布扣,bubuko.com

python下通过os模块和shutil模块进行文件处理方式

python下通过os模块和shutil模块进行文件处理方式 得到当前工作目录路径:os.getcwd() 获取指定目录下的所有文件和目录名:os.listdir(dir) 删除文件:os.remove(file) 删除多个目录:os.removedirs(r"/home") 检测路径是否为文件:os.path.isfile(path) 检测路径是否为目录:os.path.isdir(path) 判断是否为绝对路径:os.path.isabs(path) 检测路径是否存在:os.pat

python下的MySQLdb使用

python下的MySQLdb使用 3.执行sql语句和接收返回值 cursor=conn.cursor() n=cursor.execute(sql,param) 首先,我们用使用连接对象获得一个cursor对象,接下来,我们会使用cursor提供的方法来进行工作.这些方法包括两大类:1.执行命令,2.接收返回值 cursor用来执行命令的方法: callproc(self, procname, args):用来执行存储过程,接收的参数为存储过程名和参数列表,返回值为受影响的行数 execut

sae Python下设置定时任务

官方文档在这里:http://sae.sina.com.cn/doc/python/cron.html 就是通过在config.yaml文件中添加Cron段,例如: cron: - description: timing_task url: /on_time schedule: "*/5 * * * *" 代表每5分钟以get方式访问/on_time这个链接. 还可以结合sae中的Taskqueue服务把大任务分成小任务,因为sae对于每次访问有时间限制,不能超过300秒. 提醒:冒号

Python下Json和Msgpack序列化比较

Python下Json和Msgpack序列化比较  最近用Python时,遇到了序列化对象的问题,传统的json和新型序列化工具包msgpack都有涉及,于是做一个简单的总结: 通俗的讲:序列化:将对象信息转化为可以存储或传输的形式:反序列化:把这个存储的内容还原成对象. json就不用多做解释了,是一种轻量级的数据交换格式,广泛应用于web开发中.当然也是将对象序列化成符合json规范的格式.网上有一堆堆资料. 官网:http://www.json.org msgpack就有意思了,先看下官方

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

由于py3.x与工具包的兼容问题,这里采用py2.7 1.python下的复杂网络编程包networkx的使用: http://blog.sina.com.cn/s/blog_720448d301018px7.html 处理1里面提到的那四个安装包还要: 2.需要安装 setuptools: http://wenku.baidu.com/link?url=XL2qKVZbDPh-XocJW7OVZmacM4Tio5YhCyu0Uw-E7CjhiXRrhSWI4xheERjEVC3olCZ8muN

python下异常处理

1.python下异常如何处理: 1 #encoding=utf-8 2 3 """ 4 python遇到异常,程序直接运行 5 try: 6 "判断有可能抛出异常的代码" 7 print "haha" 8 except: 9 "异常下运行的代码" 10 else: 11 "运行没有异常时候的逻辑" 12 finally: 13 "不管try判断如何,该代码总会执行" 14 1

python下线程以及锁

1.python多线程 1 #encoding=utf-8 2 """ 3 python多线程,并非真正意义上的多线程 4 全局锁:在指定时间里,有且只有一个线程在运行 5 6 7 """ 8 import threading 9 import time 10 11 def test(p): 12 time.sleep(0.1) 13 print p 14 15 # a = threading.Thread(target=test) 16 # b