Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
flask with server-sent events
Search
cppgohan
January 13, 2013
Technology
4
1.8k
flask with server-sent events
sztechparty
cppgohan
January 13, 2013
Tweet
Share
More Decks by cppgohan
See All by cppgohan
ReactNative for Android first look
gohan
0
66
Introduction to Bittorrent sync
gohan
0
96
Twitter bootstrap 小试
gohan
0
100
Introduction To Hubot
gohan
1
220
Other Decks in Technology
See All in Technology
Amazon S3 Vectorsは大規模ベクトル検索を低コスト化するサーバーレスなベクトルデータベースだ #jawsugsaga / S3 Vectors As A Serverless Vector Database
quiver
2
1k
第64回コンピュータビジョン勉強会@関東(後編)
tsukamotokenji
0
190
Amazon Bedrock AgentCore でプロモーション用動画生成エージェントを開発する
nasuvitz
6
310
工業高校で学習したとあるエンジニアのキャリアの話
shirayanagiryuji
0
120
Autonomous Database Serverless 技術詳細 / adb-s_technical_detail_jp
oracle4engineer
PRO
18
52k
リモートワークで心掛けていること 〜AI活用編〜
naoki85
0
200
[CV勉強会@関東 CVPR2025 読み会] MegaSaM: Accurate, Fast, and Robust Structure and Motion from Casual Dynamic Videos (Li+, CVPR2025)
abemii
0
160
Jamf Connect ZTNAとMDMで実現! 金融ベンチャーにおける「デバイストラスト」実例と軌跡 / Kyash Device Trust
rela1470
1
210
九州の人に知ってもらいたいGISスポット / gis spot in kyushu 2025
sakaik
0
210
Intro to Software Startups: Spring 2025
arnabdotorg
0
300
新卒(ほぼ)専業Kagglerという選択肢
nocchi1
0
1.6k
意志の力が9割。アニメから学ぶAI時代のこれから。
endohizumi
1
110
Featured
See All Featured
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Writing Fast Ruby
sferik
628
62k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
36
2.5k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
We Have a Design System, Now What?
morganepeng
53
7.7k
Building a Modern Day E-commerce SEO Strategy
aleyda
43
7.4k
Producing Creativity
orderedlist
PRO
347
40k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
470
Speed Design
sergeychernyshev
32
1.1k
Side Projects
sachag
455
43k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Transcript
@cppgohan with Server-Sent Events
Flask?
Flask? MicroFramework again...
Flask? MicroFramework again... Written in Python
=
=
Werkzeug WSGI utility library =
+ Werkzeug WSGI utility library =
+ Werkzeug WSGI utility library =
+ Werkzeug WSGI utility library Jinja2 template engine =
Hello.py Demo
Hello.py Demo
Hello.py Demo
index.html Demo
index.html Demo result???
index.html Demo result??? • terminal run: python hello.py • open
http://localhost:5000
Server-Sent Event server push的html5标准, 用法简易, 单次连接比polling高效些. 相比websocket的双向二进制数据, Server-Sent Event只是单向, HTTP协议.
(但浏览器调试工具往往没法看到服务器发来的数据, X_X) Server对Client的请求, 做stream方式的响应.
Server-Sent Event
Server-Sent Event 支 持 率 不 高 , 暂 时
只 能 ”玩 ” 要 兼 容 处 理
Demo index.html
Demo hello.py
Demo redis:
Demo redis: demo:FILE_TEXT -- 每次python读出来的文本 demo:FILE_TEXT_SEQ -- 为文本版本号, 每次文本更新, 版本号+1
/sever_push响应 -- 当FILE_TEXT_SEQ增加时, 将最新的文本返回给浏览器
Demo result???
Demo result??? • terminal run: python hello.py
Demo result??? • terminal run: python hello.py • open http://localhost:5000
Demo result??? • terminal run: python hello.py • open http://localhost:5000
• block!!!
Demo result??? • terminal run: python hello.py • open http://localhost:5000
• block!!! solution???
Demo result??? • terminal run: python hello.py • open http://localhost:5000
• block!!! • async WSGI server! (eg. gunicorn + gevent, gevent) solution???
Demo result??? • terminal run: python hello.py • open http://localhost:5000
• block!!! • async WSGI server! (eg. gunicorn + gevent, gevent) • terminal run: gunicorn -k gevent -b '0.0.0.0:5000' hello:app solution???
Demo result??? • terminal run: python hello.py • open http://localhost:5000
• block!!! • async WSGI server! (eg. gunicorn + gevent, gevent) • terminal run: gunicorn -k gevent -b '0.0.0.0:5000' hello:app • open http://localhost:5000 solution???
Demo result??? • terminal run: python hello.py • open http://localhost:5000
• block!!! • async WSGI server! (eg. gunicorn + gevent, gevent) • terminal run: gunicorn -k gevent -b '0.0.0.0:5000' hello:app • open http://localhost:5000 • open http://localhost:5000 again! solution???
More Demo Server下发代码示例
More Demo • start web server • open http://localhost:5000 •
server push js script... • browser excute js script... • ... Server下发代码示例
More Usage •网站动态更新 •区分客户端推送 •微博墙实现 •webgame服务器推送 •浏览器插件
Resources • http://www.python.org/dev/peps/pep-0333/ • http://werkzeug.pocoo.org/ • http://jinja.pocoo.org/ • http://flask.pocoo.org/ •
http://www.gevent.org/ • http://gunicorn.org/ • http://dev.w3.org/html5/eventsource/ • http://www.html5rocks.com/en/tutorials/eventsource/basics/ • http://www.w3schools.com/html/html5_serversentevents.asp
END; 2013, 分享不止!