协程 && 异步例子
# 异步redis
# 在使用python代码操作redis的时候,连接、操作、断开都是网络IO。
# 安装aioredis模块:
pip install aioredis==1.3.1
# 例: 该例子使用的是aioredis==1.3.1,想要使用最新版本,请直接到pipy上查看文档。
import asyncio
import aioredis async def execute(address, passwd):
print('start', address)
redis = await aioredis.create_redis_pool(address, password=passwd)
await redis.hmset_dict('car', key1=1, key2=2,key3=3)
result = await redis.hgetall('car', encoding='utf-8')
print(result) redis.close()
await redis.wait_closed()
print('end',address)
task_list = [execute('redis://192.168.31.18', None),
execute('redis://192.168.31.18', None),]
asyncio.run(asyncio.wait(task_list)) # 异步Mysql
# 依赖模块:
sudo pip3 install asyncio
# 例:
import asyncio
import aiomysql async def execute(host, password):
print('start',host)
conn = await aiomysql.connect(host=host,port=3306, user='root', password=password, db='mysql')
cur = await conn.cursor()
await cur.execute('select * from res_user')
result = await cur.fetchall()
print(result) await cur.close()
print('end', host)
task_list = [
execute('127.0.0.1', '123456'),
execute('127.0.0.1', '123456'),
]
asyncio.run(asyncio.wait(task_list)) # 爬虫异步
import aiohttp
import asyncio async def fetch(session, url):
print('发送请求:', url)
async with session.get(url, verify_ssl=False) as response:
text = await response.text()
print('得到结果:', text)
return text
async def main():
async with aiohttp.ClientSession() as session:
url_list = [
'https://python.org',
'https://www.baidu.com',
'https://www.pythonav.com'
]
tasks = [ asyncio.create_task(fetch(session, url)) for url in url_list]
done, pending = await asyncio.wait(tasks) if __name__ == '__main__':
asyncio.run( main() )
协程 && 异步例子的更多相关文章
- python 多协程异步IO爬取网页加速3倍。
from urllib import request import gevent,time from gevent import monkey#该模块让当前程序所有io操作单独标记,进行异步操作. m ...
- 12.python进程\协程\异步IO
进程 创建进程 from multiprocessing import Process import time def func(name): time.sleep(2) print('hello', ...
- Python全栈开发-Day10-进程/协程/异步IO/IO多路复用
本节内容 多进程multiprocessing 进程间的通讯 协程 论事件驱动与异步IO Select\Poll\Epoll——IO多路复用 1.多进程multiprocessing Python ...
- Python 8 协程/异步IO
协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来 ...
- 并发 并行 进程 线程 协程 异步I/O python async
一些草率不精确的观点: 并发: 一起发生,occurence: sth that happens. 并行: 同时处理. parallel lines: 平行线.thread.join()之前是啥?落霞 ...
- Python 协程/异步IO/Select\Poll\Epoll异步IO与事件驱动
1 Gevent 协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到 ...
- 想使用gevent、mysql、sqlalchemy实现python项目协程异步达到并发的效果
如题,但是查看了很多资料,都说python这边的mysql不支持异步并发,只能阻塞进行,心塞30秒,暂时放弃这方面的研究 如果不操作数据库的化,比如请求url.操作文件,还是可以用gevent来异步实 ...
- 运筹帷幄决胜千里,Python3.10原生协程asyncio工业级真实协程异步消费任务调度实践
我们一直都相信这样一种说法:协程是比多线程更高效的一种并发工作方式,它完全由程序本身所控制,也就是在用户态执行,协程避免了像线程切换那样产生的上下文切换,在性能方面得到了很大的提升.毫无疑问,这是颠扑 ...
- 线程、进程、协程 异步io
https://www.cnblogs.com/wupeiqi/articles/6229292.html
随机推荐
- 分布式下Session一致性架构举例
一.问题及方案 见这篇文章:分布式下Session一致性问题 二.分布式环境搭建: 系统环境 [root@centos7 ~]# cat /etc/redhat-release CentOS Linu ...
- 一文看懂 ZooKeeper ,面试再也不用背八股(文末送PDF)
ZooKeeper知识点总结 一.ZooKeeper 的工作机制 二.ZooKeeper 中的 ZAB 协议 三.数据模型与监听器 四.ZooKeeper 的选举机制和流程 本文将以如下内容为主线讲解 ...
- SyntaxError: Non-UTF-8 code starting with '\xef' in file(已解决)
错误原因: python代码中出现了中文字符 解决方案: 在python代码文件的第一行(必须是第一行)添加如下代码(随编码不同自行修改): #coding=utf-8
- 浅析kubernetes中client-go Informer
之前了解了client-go中的架构设计,也就是 tools/cache 下面的一些概念,那么下面将对informer进行分析 Controller 在client-go informer架构中存在一 ...
- 记一次Tomcat卡死在 Deploying web application 步骤的问题
公司有一个历史的遗留项目是传统的MVC架构的前后不分离的项目,一开始使用JDK1.7写的,后来前一阵老板说想在这个远古项目上加点功能,顺带换换皮,于是乎一帮程序员们就用JDK1.8重新翻新了一遍项目顺 ...
- USACO 刷题小记
\(\text{High Card Low Card}\) USACO2015DEC Platinum T2 贝西和艾尔西在玩游戏.有 \(2n\) 张牌,牌上的数字是 \(1\) 到 \(2n\) ...
- jeecgboot-vue3笔记(八)——treeSelect树形选择组件的使用(一次性加载)
使用效果 前端代码 定义interface export interface TreeDataItem { value: string; key: string; title?: string; sl ...
- lanedet项目调试记录
苦水时间:最近深度学习调代码真的是调的郁闷,每次调都是旧的问题没有解决,新的问题又冒出来了.新的好不容易解决了,旧的问题还是没有解决思路解决不了. 正文 最近找到一个实现了很多车道线检测算法的gith ...
- 数位 dp 总结
数位 dp 总结 特征 问你一个区间 \([L,R]\) 中符合要求的数的个数 一个简单的 trick :把答案拆成前缀和 \(Ans(R)-Ans(L-1)\) 如何求 \(Ans()\) ,就要用 ...
- C语言- 基础数据结构和算法 - 栈的顺序存储
听黑马程序员教程<基础数据结构和算法 (C版本)>, 照着老师所讲抄的, 视频地址https://www.bilibili.com/video/BV1vE411f7Jh?p=1 喜欢的朋友 ...