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
930
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
710
2017 - Opening up to Open Source
pybay
0
250
2017 - A Gentle Introduction to Text Classification with Deep Learning
pybay
2
190
2017 - Performant Asynchronous Programming at Quora
pybay
1
380
2017 - latus - a Personal Cloud Storage App written in Python
pybay
2
520
2017 - Everything You Ever Wanted to Know About Web Authentication in Python
pybay
3
630
Featured
See All Featured
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
53
2.9k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
54k
Site-Speed That Sticks
csswizardry
10
690
Navigating Team Friction
lara
187
15k
The Pragmatic Product Professional
lauravandoore
35
6.7k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Typedesign – Prime Four
hannesfritz
42
2.7k
Java REST API Framework Comparison - PWX 2021
mraible
31
8.7k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
20k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
5.9k
Embracing the Ebb and Flow
colly
86
4.7k
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