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 GitHub (no longer) Works
holman
314
140k
A Tale of Four Properties
chriscoyier
160
23k
We Have a Design System, Now What?
morganepeng
53
7.7k
Producing Creativity
orderedlist
PRO
346
40k
Typedesign – Prime Four
hannesfritz
42
2.7k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
2.9k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
The Invisible Side of Design
smashingmag
301
51k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
Gamification - CAS2011
davidbonilla
81
5.4k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.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