Python学习---IO的异步[asyncio模块(no-http)]
Asyncio进行异步IO请求操作:
1. @asyncio.coroutine 装饰任务函数
2. 函数内配合yield from 和装饰器@asyncio.coroutine 配合使用【固定格式】
3. loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*tasks)) # 接受异步IO的任务并异步执行任务
实例一:
异步IO: 协程机制 + 回调函数
import asyncio @asyncio.coroutine # 装饰任务函数
def func1():
print('before...func1......')
# yield from 和装饰器@asyncio.coroutine 配合使用【固定格式】
yield from asyncio.sleep(5) # 必须写asyncio才表示异步IO执行5秒,time.sleep(5)不生效
print('5秒后...')
print('end...func1......') tasks = [func1(), func1()]
# 事件循环: 对涉及异步,协成,阻塞等IO操作时进行事件的循环操作
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(*tasks)) # 接受异步IO的任务并异步执行任务
loop.close()
AsyncIO缺点:
不支持HTTP请求,也就是说不能直接发送URL过去进行访问
支持TCP请求,也就是说可以发送【IP+端】进行访问
注:HTTP在TCP之上
基于asyncio实现利用TCP模拟HTTP请求
import asyncio
# 基于asyncio实现利用TCP模拟HTTP请求[asyncio实际上不支持HTTP请求]
@asyncio.coroutine
def fetch_async(host, url='/'):
print('HOST和URL信息:', host, url)
# reader: 用于读取连接的信息
# writer: 用于给服务器写信息
reader, writer = yield from asyncio.open_connection(host, 80)
# 基于TCP模拟的HTTP请求:请求头header和请求体body之间是2空行【\r\n\r\n】分隔的
request_header_content = """GET %s HTTP/1.0\r\nHost: %s\r\n\r\n""" % (url, host,)
request_header_content = bytes(request_header_content, encoding='utf-8') # 字符串转换字节 writer.write(request_header_content) # 准备发送数据给服务器
# drain: 英文翻译为喝光,这里作发送完成理解
yield from writer.drain() # 发送数据给服务器,此时可能会阻塞执行个请求,考虑数据量大等原因
text = yield from reader.read() # 等待返回的数据,text就是先收到回复的请求完成后等待其他返回
print(host,url,'返回后的结果:', text)
writer.close() # 关闭流 tasks = [
fetch_async('www.cnblogs.com', '/ftl1012/'),
fetch_async('www.dig.chouti.com', '/images/homepage_download.png')
] loop = asyncio.get_event_loop()
results = loop.run_until_complete(asyncio.gather(*tasks))
loop.close()
基于TCP模拟HTTP详解:
Python学习---IO的异步[asyncio模块(no-http)]的更多相关文章
- Python学习---IO的异步[asyncio +aiohttp模块]
aiohttp aiohttp是在asyncio模块基础上封装的一个支持HTTP请求的模块,内容比8.4.2[基于asyncio实现利用TCP模拟HTTP请求]更全面 安装aiohttp: pip3 ...
- Python学习---IO的异步[tornado模块]
tornado是一个异步非阻塞的WEB框架.它的异步非阻塞实际上就是用事件循环写的. 主要体现在2点: 1. 作为webserver可以接收请求,同时支持异步处理请求.Django只能处理完成上一个请 ...
- Python学习---IO的异步[twisted模块]
安装twisted模块 Linux: pip3 install twisted Window: a. http://www.lfd.uci.edu/~gohlke/pythonlibs/#twiste ...
- Python学习---IO的异步[gevent+Grequests模块]
安装gevent模块 pip3 install gevent Gevent实例 import gevent import requests from gevent import monkey # so ...
- Python学习---IO的异步[自定义异步IO]
自定义IO异步基础知识: --所有的请求都基于socket实现,一个请求就是一个socket socket.setblocking(False) 不需要阻塞,一个请求完了发送另外一个,会报错,需解决 ...
- Python学习系列(六)(模块)
Python学习系列(六)(模块) Python学习系列(五)(文件操作及其字典) 一,模块的基本介绍 1,import引入其他标准模块 标准库:Python标准安装包里的模块. 引入模块的几种方式: ...
- python学习第四十八天json模块与pickle模块差异
在开发过程中,字符串和python数据类型进行转换,下面比较python学习第四十八天json模块与pickle模块差异. json 的优点和缺点 优点 跨语言,体积小 缺点 只能支持 int st ...
- Python学习day17-常用的一些模块
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- 【Python学习之九】asyncio—异步IO
asyncio 这是python3.4引入的标准库,直接内置对异步IO的支持.asyncio的编程模型就是一个消息循环.从asyncio模块中直接获取一个EventLoop的引用,然后把需要执行的协程 ...
随机推荐
- Go 压测
1. 单测 + 压测 压测 go test -bench=. -benchmem 单元测试 go test -v . 2. pprof + 火焰图(查看cpu占用,内存占用) 嵌入代码 import ...
- 外网访问不了Xampp(本地访问不了虚拟机的Xampp)
安装好了Xampp,在虚拟机是可以访问的, 浏览器中输入localhost 嘛 不过在本地就是访问不了,ping是能通过的 然后网上查了一些资料,并结合Xampp的提示: 特别注意:如果连上面这 ...
- 链式编程:遇到多个构造器参数(Constructor Parameters)时要考虑用构建器(Builder)
public class NutritionFacts { private final int servingSize; private final int servings; private fin ...
- URL编码分析与乱码解决方案
一.问题的由来 URL就是网址,只要上网,就一定会用到. 一般来说,URL只能使用英文字母.阿拉伯数字和某些标点符号,不能使用其他文字和符号.比如,世界上有英文字母的网址"http://ww ...
- Core中使用Hangfire
之前使用Quartz.Net,后来发现hangfire对Core的继承更加的好,而且自带管理后台,这就比前者好用太多了. 安装注册 安装 PM> Install-Package Hangfire ...
- Selenium私房菜系列4 -- Selenium IDE的使用
(转自http://www.cnblogs.com/hyddd/archive/2009/05/24/1487967.html) 前面说过,Selenium IDE是Firefox的一个插件,是可以进 ...
- BATJ面试必会之 Spring 篇(一)
译者:深海 校对:方腾飞 出自并发编程网 – ifeve.com 目录 Spring 概述 依赖注入 Spring beans Spring注解 Spring数据访问 Spring面向切面编程(AOP ...
- [转]Global exception handling in Web API 2.1 and NLog
本文转自:https://stackoverflow.com/questions/25865610/global-exception-handling-in-web-api-2-1-and-nlog ...
- jQuery ajax - getJSON() 用法实例
实例 从 test.js 载入 JSON 数据并显示 JSON 数据中一个 name 字段数据: $.getJSON("test.js", function(json){ aler ...
- AutoFac使用方法总结二:事件与依赖循环
事件 AutoFac支持三种事件:OnActivating,OnActivated,OnRelease.OnActivating在注册组件使用之前会被调用,此时可以替换实现类或者进行一些其他 ...