Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
PyCon2014China-Zhuhai-jeff
Search
Zoom.Quiet
November 17, 2014
Technology
160
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PyCon2014China-Zhuhai-jeff
141115 in Zhuhai
http://zoomq.qiniudn.com/CPyUG/PyCon2014China/141115zh-pm8-jeff.MP3
Zoom.Quiet
November 17, 2014
More Decks by Zoom.Quiet
See All by Zoom.Quiet
PyCon2014China-Zhuhai-high performance
zoomquiet
0
210
PyCon2014China-Zhuhai-meta programming
zoomquiet
1
180
PyCon2014China-Zhuhai-bpm.py
zoomquiet
0
160
PyCon2014China-Zhuhai-luna kv db
zoomquiet
0
160
PyCon2014China-Zhuhai-seed studio
zoomquiet
0
160
PyCon2014China-Zhuhai-Docker Registry Build By Python
zoomquiet
0
170
PyCon2014China-Zhuhai-pythonic front-end
zoomquiet
0
210
DevFest2014-Zhuhai-Polymer
zoomquiet
0
500
TEDxJLUZH MOMENT future
zoomquiet
0
480
Other Decks in Technology
See All in Technology
フィンテック養成勉強会#68
finengine
0
240
AI時代の開発はウォーターフォールに回帰する? / development-with-ai-back-to-wf
okamototakuyasr2
0
110
Filter What the LLM Reads: RAG Governance with AgentCore Gateway
yakumo
0
110
AWSセキュリティで活用するAI機能
cmusudakeisuke
1
310
(swiftc) NonCopyableとライフタイム修飾子
teamhimeh
0
130
CTO Night|NotionのAIエージェントとFDE
kzkhykw1991
0
320
LLM Content Moderationを支える評価・改善ループの実践
suikabar
2
360
小規模OSSモデルは サイバーセキュリティ業務に どこまで使えるか
bbrbbq
0
300
AI駆動開発におけるハーネスエンジニアリングの勘所
tsukuboshi
1
780
演劇と私とスクラム
chinmo
0
160
渡米前の準備を制す!re:Invent 2026徹底準備術 / The Ultimate Preparation Guide for re:Invent 2026
yuj1osm
1
250
AWSDirectConnectの構築の進め方を整理してみた
masa_my
1
190
Featured
See All Featured
AI Search: Where Are We & What Can We Do About It?
aleyda
0
8.1k
Testing 201, or: Great Expectations
jmmastey
46
8.4k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
600
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
New Earth Scene 8
popppiees
4
2.7k
Mind Mapping
helmedeiros
1
410
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.5k
Building the Perfect Custom Keyboard
takai
2
920
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
70
67k
Mobile First: as difficult as doing things right
swwweet
225
10k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Transcript
None
万万没想到之: Python Web异步编程记
[email protected]
本栏⺫⽬目由“珠三⾓角技术沙⻰龙”赞助播出 蕉爷,你姓蕉吗? Yeah!爷姓蕉!
本栏⺫⽬目由“⾳音乐说”赞助播出 觉得我好听就闪聊⼀一下呗
本栏⺫⽬目由“萌叔漫玩”赞助播出 ⼩小妹妹,你看⼤大叔萌萌哒,有⽊木有
@jeff_kit @姐夫 前珠三⾓角技术沙⻰龙⾸首席组委 Toraysoft co-founder Toraysoft ⾸首席码农
举个栗⼦子开始 = / A ? / 0 0 / 2
听题:写⼀一个搜索歌曲的接⼝口
Django ⼀一下 uwsgi --http :8000 --module pyconf2014.wsgi --processes 1
ab之 500秒!
改进#1 uwsgi --http :8000 --module pyconf2014.wsgi --processes 10
再ab之 50秒!
嗯!启动100条进程就 可以了!
问题在哪? • 同步⺴⽹网络请求耗时⻓长,前⾯面的请求导致后续的请求阻 塞。 • 最⼤大进程数就是最⼤大并发数。 • 消耗巨⼤大的服务器资源。 • 请求队列爆掉,如同被DDOS!
异步解决⽅方案 • ⾮非阻塞IO + 异步调度 • select,epoll,kqueue
说⼈人话 • web服务器请求第三⽅方服务的时候,交出控制权,返 回去受理新的⽤用户请求。 • 第三⽅方服务有返回的时候,通知web服务器进程,把 结果返回给⽤用户。
? / I A= K / 0 02 / 0
02 A= K
异步web框架哪家强? • 没有异步web框架,wsgi是同步标准。 • 那是web server的事⼉儿。
异步webserver哪家强? • twisted,超重量级。略。 • Tornado,既是server,也是framework。 • Gevent兼容的uwsgi Contailer,uWsgi,Gunicron。
Tornado
作为Server python run_tornado.py
ab⼀一下 5秒!
回调?⽆无爱!耶!yield!
优点 • 单线程,⾼高并发 • 低能耗,⾼高效率
可惜 • 回调模型⽆无爱 • yield伪同步模式⼊入侵性太强 (第三⽅方sdk要重新实 现) • server与框架耦合度太⾼高,⼊入侵性太强+1, 不能使⽤用
熟悉的框架作业。 • 作为wsgi server,未能发挥其优势
Gevent! • Greenlet • libEvent • monkey_patch()!
认识Gevent
Gevent能 • 让你按原来的⽅方式写代码。 • 让同步编写的代码,异步地执⾏行。 • 让单线程也并发,且⽐比多线程更⾼高效环保。 • 让原⽣生的thread变成green thread。
Gevent + wsgi • uWsgi
uWsgi • 修改处理器的类型即可 processes => gevent • patch all:— gevent-monkey-patch
代码,还是原来的味 uwsgi --http :8000 --module pyconf2014.wsgi --gevent 100 --gevent-monkey-patch
再ab⼀一下 5秒!
但,uWsgi + Django的泪 • uWsgi的对thread的patch有问题 • 如果django的view有访问数据库,则数据库连接⾮非 线程(协程)安全。 • databasewrapper
objects created in a thread can only be used in that same thread。
Gunicron + Django • Gunicron,patch完美 • 但,使⽤用Django数据库连接池需谨慎!⾼高并发导致 占⽤用⼤大量的数据库连接。
万万没想到 python web异步化,配个参数就完了
谢谢