当前位置:网站首页>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
边栏推荐
- 现货黄金操作技巧_估波曲线
- 华为机试--高精度整数加法
- 现货黄金基本介绍
- [AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
- 【Pytorch基础】torch.split()用法
- Go 语言中的 logger 和 zap 日志库
- 【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
- UDP protocol and TCP protocol
- Who will answer the question?
- Jetpack 之 LifeCycle 组件使用详解
猜你喜欢
HMS Core Discovery第14期回顾长文|纵享丝滑剪辑,释放视频创作力
基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
[mapping program design] coordinate azimuth calculation artifact (version C)
[AI vision · quick review of robot papers today, issue 31] Fri, 15 APR 2022
补:注解(Annotation)
Difference between LabVIEW small end sequence and large end sequence
【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
Hard core chip removal
STM32 upper μ C / shell transplantation and Application
随机推荐
Express中间件①(中间件的使用)
兼容NSR20F30NXT5G的小体积肖特基二极管
STM32上μC/Shell移植与应用
The difference between lists, tuples, dictionaries and collections
AWS EKS 部署要点以及控制台与eksctl创建的差异
Who will answer the question?
Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
KVM error: Failed to connect socket to ‘/var/run/libvirt/libvirt-sock‘
电钻、电锤、电镐的区别
STM32单片机ADC规则组多通道转换-DMA模式
小红书被曝整体裁员20%,大厂之间内卷也很严重
matlab读取多张fig图然后合并为一张图(子图的形式)
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
IEEE Transactions on Industrial Informatics(TII)投稿须知
【NeurIPS 2019】Self-Supervised Deep Learning on Point Clouds by Reconstructing Space
【时序】基于 TCN 的用于序列建模的通用卷积和循环网络的经验评估
mysql table 中增加列的SQL语句
VSCode配置之Matlab极简配置
【BIM入门实战】Revit中的墙体层次以及常见问题解答
TreeSet课后练习