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
2016 - Ramesh Sampath - Building a Tic-Tac-Toe ...
Search
PyBay
September 25, 2016
0
77
2016 - Ramesh Sampath - Building a Tic-Tac-Toe Two-Player Game using Tornado over Websockets
PyBay
September 25, 2016
Tweet
Share
More Decks by PyBay
See All by PyBay
2017 - The Packaging Gradient
pybay
2
940
2017 - Building Bridges: Stopping Python 2 without damages
pybay
0
660
2017 - Bringing Python 3 to LinkedIn
pybay
1
570
2017 - Python Debugging with PUDB
pybay
0
720
2017 - Opening up to Open Source
pybay
0
260
2017 - A Gentle Introduction to Text Classification with Deep Learning
pybay
2
200
2017 - Performant Asynchronous Programming at Quora
pybay
1
390
2017 - latus - a Personal Cloud Storage App written in Python
pybay
2
530
2017 - Everything You Ever Wanted to Know About Web Authentication in Python
pybay
3
640
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.9k
Making the Leap to Tech Lead
cromwellryan
134
9.5k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
50
5.5k
A Tale of Four Properties
chriscoyier
160
23k
Site-Speed That Sticks
csswizardry
10
770
Typedesign – Prime Four
hannesfritz
42
2.7k
Adopting Sorbet at Scale
ufuk
77
9.5k
Java REST API Framework Comparison - PWX 2021
mraible
33
8.8k
Embracing the Ebb and Flow
colly
86
4.8k
Why Our Code Smells
bkeepers
PRO
337
57k
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.5k
Build your cross-platform service in a week with App Engine
jlugia
231
18k
Transcript
Tic-Tac-Toe App with Tornado & WebSockets By Ramesh Sampath APP:
http://games.sampathweb.com
Ramesh Sampath • Engineer ◦ Build Applications ◦ Build Machine
Learning models ◦ Deploy @sampathweb (github / twitter / linkedin)
Objective Build and Deploy a Tic-Tac-Toe App that we can
play with Friends.
Audience • New to WebSockets • May have built some
Webapps • Want to built a Game / Chat App
Agenda • Tic-Tac-Toe - Single Player • Two Player Game
• WebSockets • Tornado • Deploy • Scaling
Tic-Tac-Toe App (Command Line)
Tic Tac Toe Source: Wikipedia
Demo Command Line Version
Tic-Tac-Toe - Play with Human
Two Player Version Web Server Want to Play Other
Web App Clients Web Server Request Response Web App (Python)
Web App • Request / Response • Scale to lots
of connections ◦ Short lived ◦ Isolated • Flask / Django over WSGI / Gunicorn & Nginx
Two Player Version Web Server Want to Play Other
Web App • Ajax Long Polling ◦ Latency ◦ Polling
Frequency ◦ Server cannot initiate Push • Blocking - One Request at a Time.
WebSockets
WebSockets WebSocket is a protocol providing full-duplex communication channels over
a single TCP connection. (RFC 6455)
WebSockets • Tunnel Data (String, Blob, ArrayBuffer) ◦ JSON ◦
XML / HTML ◦ Images, Sound, Video
WebSockets API • Open ◦ var ws = new WebSocket(ws://<endpoint>)
• Send Message ◦ ws.send() • Receive Message ◦ ws.onmessage()
WebSockets Handler Open Socket Connection • Send Message • Receive
Messages • Close Connection Handler Handler Handler Handler Application G A M E S T A T E
WebSockets • Persistent Connection • Two-way communication • Handle Lots
of Connections • Non-Blocking (Async)
Async Stack Credits: Anton Caceres - Better asynchronous code with
Tornado and Python 3 [EuroPython 2015]
Tornado • Web Framework • Single Threaded • Non-Blocking I/O
Concurrency • Thousands of Requests
Tornado - When to use it • Building REST APIs
(GET / POST) • Micro Services • Slow database queries • Communicating with external resources • Need to handle large number of connections
Demo!
Code Walkthrough Open Text Editor
Things to Lookout For • Async entire code ◦ Database
Calls - Momoko over SqlAlchemy ◦ External HTTP Calls - AsyncHTTP ◦ Non Async Libraries ▪ Wrapped in coroutines / Threadpool
Deploy to AWS
Deploy Steps • Deploy Notes in the “README.md” file of
repo. https://github.com/sampathweb/board-games-app
Deployment Handler Application S T A T E Handler Handler
Handler Supervisor Process
Scaling App Handler Application S T A T E Handler
Handler Handler Handler Application S T A T E Handler Handler Handler Not Connected
Problems • Single Instance of App ◦ Latency ◦ Number
of connections • Source: Singleton Manager to maintain State
Scale with Pub/Sub Load Balancer (Nginx) Clients Tornado Process #1
Tornado Process #2 Tornado Process #3 Tornado Process #4 Supervisor Process Pub / Sub (Redis)
Thank You! Slides: https://goo.gl/NKzuZB @sampathweb (Github / Twitter / Linkedin)
WebSockets Web Server Web App (Python) Client Client Client Client
Starts with HTTP Handshake