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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
200
PyCon2014China-Zhuhai-meta programming
zoomquiet
1
180
PyCon2014China-Zhuhai-bpm.py
zoomquiet
0
160
PyCon2014China-Zhuhai-luna kv db
zoomquiet
0
150
PyCon2014China-Zhuhai-seed studio
zoomquiet
0
150
PyCon2014China-Zhuhai-Docker Registry Build By Python
zoomquiet
0
170
PyCon2014China-Zhuhai-pythonic front-end
zoomquiet
0
190
DevFest2014-Zhuhai-Polymer
zoomquiet
0
490
TEDxJLUZH MOMENT future
zoomquiet
0
450
Other Decks in Technology
See All in Technology
なぜ「決定性」が 決定的に重要なのか? Durable Execution 基盤の数理的理解 #serverlessjp / ServerlessDays Tokyo 2026
ytaka23
0
170
アプリをもっと"iOSアプリっぽく"する小さな工夫 / Small Touches That Make Your App Feel More Like an iOS App
matsuji
1
910
安心して変更できるWebフロントエンドの作り方
pirosikick
4
2.6k
2026-09-18 gotanda.sre Terraformで複数環境作ったり、複数Stateに分割したりそれとTerragrunt / Terraform multi envs and multi states
masasuzu
1
400
Claude Code本って、 読む必要あるの?
oikon48
2
470
越境するなら専門用語を使うな高校校歌 / If you wanna cross border, you shouldn't use jargon
vtryo
0
140
AI時代、データエンジニアが一番おもろい
genshun9
0
610
新機種発売前に見直そう!端末移行で再ログインが要るアプリ・要らないアプリは何が違うのか 〜シームレスに再開できる設計と実装〜
zozotech
PRO
0
200
[2026-09-11]SREは誰のもの?運用エンジニアが始める 「SRE領域への越境」とチームの進化の軌跡 〜Road to NEXT CRE
tosite
0
280
AI 時代のスタートアップエコシステ厶から考究する技術的負債との向き合い方
m3m0r7
PRO
2
2.1k
ソフトウェアDNAとクラウドエージェントのススメ
cloudace
0
110
AgentCore Runtime上にAgentic Coding基盤を構築・展開する際の設計ポイントと限界点 / Design considerations and limitations when building an agentic coding platform on AgentCore Runtime
har1101
2
130
Featured
See All Featured
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
120
120k
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.5k
We Are The Robots
honzajavorek
0
370
Scaling GitHub
holman
464
140k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.6k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.4k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
440
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Building Applications with DynamoDB
mza
96
7.2k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
How GitHub (no longer) Works
holman
316
150k
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异步化,配个参数就完了
谢谢