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
150
PyCon2014China-Zhuhai-Docker Registry Build By Python
zoomquiet
0
170
PyCon2014China-Zhuhai-pythonic front-end
zoomquiet
0
210
DevFest2014-Zhuhai-Polymer
zoomquiet
0
490
TEDxJLUZH MOMENT future
zoomquiet
0
480
Other Decks in Technology
See All in Technology
LLM機能を自作して分かるSnowflake Cortex AIの強み
nayuts
0
290
SREでアラート疲れを 解決しよう!
kairim0
1
200
オブザーバビリティを高める AI エージェント体験を考える / Designing AI Agent Experiences That Enhance Observability
aoto
PRO
2
300
個別開発で終わらせない。 現場の課題をプロダクトの強さに変える StockmarkのFDE
ktkrhr
0
590
おそらく日本で唯一のDevRelインターン生として
husengs7
0
170
[2026-09-30]ロックンロールは鳴り止まないっ - 信頼性かまってちゃん - 「データ駆動を投げ捨ててまで。」追いかける信頼性改善に向けた取り組みの話
tosite
0
200
【データ横丁主催】AI Agentがコンテキストを使って仕事をした後、何が残るのか― 組織の経験を次の判断に引き継ぐ「Agent Memory」
shisyu_gaku
2
330
OSC2026on_the-world-is-waiting-for-your-voice.pdf
naruoga
0
350
Snapshot Testing in Practice: Predictable and Reliable SwiftUI Views
fespinoza
0
120
生成AIで高い生産性を求められて、 悩み、乗り越えた話
mot_techtalk
2
130
AI Made Us Faster at Solving the Wrong Problems
marceloancelmo
0
160
「とりあえず動く」の先へ。 AI時代のチーム開発と内部設計/2026-slsdays
slsops
0
150
Featured
See All Featured
Music & Morning Musume
bryan
48
7.4k
Utilizing Notion as your number one productivity tool
mfonobong
4
610
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
350
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
Context Engineering - Making Every Token Count
addyosmani
9
1.2k
Designing for humans not robots
tammielis
254
26k
Prompt Engineering for Job Search
mfonobong
0
480
Optimising Largest Contentful Paint
csswizardry
37
4k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
エンジニアに許された特別な時間の終わり
watany
109
250k
KATA
mclloyd
PRO
35
16k
Discover your Explorer Soul
emna__ayadi
2
1.3k
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异步化,配个参数就完了
谢谢