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
70
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
880
2017 - Building Bridges: Stopping Python 2 without damages
pybay
0
620
2017 - Bringing Python 3 to LinkedIn
pybay
1
540
2017 - Python Debugging with PUDB
pybay
0
660
2017 - Opening up to Open Source
pybay
0
240
2017 - A Gentle Introduction to Text Classification with Deep Learning
pybay
2
170
2017 - Performant Asynchronous Programming at Quora
pybay
1
360
2017 - latus - a Personal Cloud Storage App written in Python
pybay
2
490
2017 - Everything You Ever Wanted to Know About Web Authentication in Python
pybay
3
580
Featured
See All Featured
Docker and Python
trallard
44
3.3k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
49
2.3k
GitHub's CSS Performance
jonrohan
1030
460k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
4
440
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
366
25k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Building Adaptive Systems
keathley
40
2.4k
BBQ
matthewcrist
87
9.5k
Building an army of robots
kneath
303
45k
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