当前位置:网站首页>The perfect combination of collaborative process and multi process
The perfect combination of collaborative process and multi process
2022-04-23 04:27:00 【VIP_ CQCRE】
This is a 「 Attacking Coder」 Of the 607 Technology sharing
author :kingname
source : Unheard of Code
“
It is necessary to read this article 4 minute .
” We know , A coroutine is essentially a single thread, single process , By making the most of IO Wait time to achieve high concurrency . stay IO Code outside the waiting time , Or serial operation . therefore , If there are many processes , How many serial codes in each collaboration run longer than IO Waiting time for request , There will be an upper limit on concurrency .
for instance , Cook in an electric cooker , Washing clothes with a washing machine , Boil water in a hot kettle , They all start the equipment and then they can run it by themselves , We can use their own running time , Make these three things seem to be going on almost at the same time . But if apart from these three things , And turn on the TV , turn the air conditioner on , Send wechat …… Wait, dozens of things . It really only needs to be a beginning when everything is taken out alone , The rest is to wait , But because it takes time to start , So it will take a lot of time to start them all , Your efficiency is still stuck .
Now? , If there are two people doing these things together , That's not the case . Cook and boil water alone , The other man turns on the washing machine , Turn on the TV and air conditioning . Efficiency has been further improved .
This is the combination of cooperative process and multi process , Multiple coprocesses in each process run at the same time , make the best of CPU Every core of , Make the most of it again IO Waiting time , hold CPU Run full , Run the network bandwidth full . Strong alliance , Faster .
There is a third-party library aiomultiprocess
, It allows you to realize the combination of multi process and collaborative process in a few lines of code .
use first pip install :
python3 -m pip install aiomultiprocess
Its grammar is very simple :
from aiomultiprocess import Pool
async with Pool() as pool:
results = await pool.map( coroutines , parameter list )
It only needs 3 Line code , It'll be there for you CPU Start a process on each core , Start the process continuously in each process .
Let's write a piece of actual code :
import asyncio
import httpx
from aiomultiprocess import Pool
async def get(url):
async with httpx.AsyncClient() as client:
resp = await client.get(url)
return resp.text
async def main():
urls = [url1, url2, url3]
async with Pool() as pool:
async for result in pool.map(get, urls):
print(result) # every last URL What's coming back
if __name__ == '__main__':
asyncio.run(main())
When I was writing asynchronous collaboration articles before , Some of my classmates will ask me , Is the speed of a reptile really that important ? Isn't it the most important thing to break through anti reptiles ?
My answer is , Don't see the use of aiohttp Asking for a website feels like being a crawler . In microservices , Ask for your own HTTP Interface , Also need to use httpx perhaps aiohttp. In such a scene , Speed is very important , Sometimes it just needs to be done as soon as possible .
About aiomultiprocess
More use of , You can refer to its official documentation [1].
reference
[1] Official documents : https://aiomultiprocess.omnilib.dev/en/latest/guide.html
End
Cui Qingcai's new book 《Python3 Web crawler development practice ( The second edition )》 It's officially on the market ! The book details the use of zero basis Python Develop all aspects of reptile knowledge , At the same time, compared with the first edition, it has added JavaScript reverse 、Android reverse 、 Asynchronous crawler 、 Deep learning 、Kubernetes Related content , At the same time, this book has obtained Python The father of Guido The recommendation of , At present, this book is on sale at a 20% discount !
Content introduction :《Python3 Web crawler development practice ( The second edition )》 Content introduction
Scan purchase
Haowen and his friends watch ~
版权声明
本文为[VIP_ CQCRE]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230426376663.html
边栏推荐
- 520.检测大写字母
- PHP export excel table
- [AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022
- Microbial neuroimmune axis -- the hope of prevention and treatment of cardiovascular diseases
- [mapping program design] coordinate azimuth calculation artifact (version C)
- Xiaohongshu was exposed to layoffs of 20% as a whole, and the internal volume among large factories was also very serious
- 阿里云IoT流转到postgresql数据库方案
- C语言常用字符串处理函数
- Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
- Bridge between ischemic stroke and intestinal flora: short chain fatty acids
猜你喜欢
为什么推荐你学嵌入式
Chlamydia infection -- causes, symptoms, treatment and Prevention
OpenCV----YOLACT实例分割模型推理
【Echart】echart 入门
阿里云IoT流转到postgresql数据库方案
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
用D435i录制自己的数据集运行ORBslam2并构建稠密点云
Qtspim manual - Chinese Translation
Opencv -- yoact case segmentation model reasoning
[mapping program design] coordinate inverse artifact v1 0 (with C / C / VB source program)
随机推荐
What is software acceptance testing? What are the benefits of acceptance testing conducted by third-party software testing institutions?
AWS EKS添加集群用户或IAM角色
TreeSet after class exercises
MYSQL查询至少连续n天登录的用户
C语言: 指针的进阶
小红书被曝整体裁员20%,大厂之间内卷也很严重
Set classic topics
Xshell、Xftp连接新创建的Unbutu系统虚拟机全流程
QT program integration easyplayer RTSP streaming media player screen flicker what is the reason?
三十六计是什么
Introduction to Cortex-M3 register set, assembly language and C language interface
单极性非归零NRZ码、双极性非归零NRZ码、2ASK、2FSK、2PSK、2DPSK及MATLAB仿真
单片机串口数据处理(2)——uCOSIII+循环队列接收数据
Effects of antibiotics on microbiome and human health
The difference between lists, tuples, dictionaries and collections
Opencv -- yoact case segmentation model reasoning
Fusobacterium -- symbiotic bacteria, opportunistic bacteria, oncobacterium
The whole process of connecting the newly created unbutu system virtual machine with xshell and xftp
【李宏毅2022 机器学习春】hw6_GAN(不懂..)
Jetpack 之 LifeCycle 组件使用详解