Download - Communication in Python and the C10k problem
![Page 1: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/1.jpg)
Jose Ignacio Galarza @igalarzab
Communication in Python and the C10k problem
![Page 2: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/2.jpg)
MANOLOOOOO!!!!
![Page 3: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/3.jpg)
![Page 4: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/4.jpg)
select the best method to
communicate
![Page 5: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/5.jpg)
select the best method to scale
![Page 6: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/6.jpg)
![Page 7: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/7.jpg)
Index✤ From polling… to pushing ✤ Concurrency ✤ C10k ✤ Asynchronous I/O
![Page 8: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/8.jpg)
PYTHON 3
PYTHON 3 EVERYWHERE
![Page 9: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/9.jpg)
From polling to pushing
![Page 10: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/10.jpg)
polling
Client Server
![Page 11: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/11.jpg)
polling➡ Nothing new to implement ➡ Good support in all platforms ➡ Proxy friendly ➡ Uni-directional ➡ Very low efficiency ➡ No cross-domain
![Page 12: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/12.jpg)
while True: r = requests.get('http://api.yo.com/messages') messages.extend(r.json()) time.sleep(5)
[email protected]("/messages") def messages(): message = check_new_message() return message
![Page 13: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/13.jpg)
long polling
ServerClient
![Page 14: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/14.jpg)
long polling➡ Good support in all platforms ➡ “Proxy friendly” ➡ Uni-directional ➡ Low efficiency (better than polling) ➡ No cross-domain
![Page 15: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/15.jpg)
long polling
while True: try: r = requests.get('http://yo.com/messages', timeout=60) except TimeOut: continue
messages.extend(r.json())
@app.route("/messages") def messages(): message = wait_until_new_message() return message
![Page 16: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/16.jpg)
HTTP Streaming
ServerClient
![Page 17: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/17.jpg)
HTTP Streaming➡ Better efficiency than (long) polling ➡ “Proxy friendly” ➡ Uni-directional ➡ You need to parse the data manually
![Page 18: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/18.jpg)
HTTP Streaming➡ Type 1: Connection Close
HTTP/1.1 200 OK Content-Type: text/plain Connection: close
Hello world This a connection close response
![Page 19: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/19.jpg)
HTTP Streaming➡ Type 2: Chunked response
HTTP/1.1 200 OK Content-Type: text/plain Transfer-Encoding: chunked
E Hello World!
19 I am a chunked response
0
![Page 20: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/20.jpg)
HTTP Streaming
r = requests.get('http://yo.com/messages', stream=True) for message in r.iter_lines(): messages.extend(message.decode(‘utf8’))
@app.route("/messages") def messages(): def content(): for sentence in wait_until_next_sentence(): yield sentence
return Response(content())
![Page 21: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/21.jpg)
SSE
ServerClient
![Page 22: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/22.jpg)
SSE➡ Well known protocol (HTTP) ➡ Good efficiency ➡ JS API ➡ Few client implementations ➡ Uni-directional
![Page 23: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/23.jpg)
WebSockets
ServerClient
![Page 24: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/24.jpg)
WebSockets➡ Bidirectional ➡ Great efficiency ➡ A lot of implementations (JS API) ➡ Cross-domain ➡ Handshake to update from HTTP ➡ Complete different protocol
![Page 25: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/25.jpg)
GET /chat HTTP/1.1 Host: server.example.com Upgrade: websocket Connection: Upgrade Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw== Sec-WebSocket-Protocol: chat, superchat Sec-WebSocket-Version: 13 Origin: http://example.com
WS Handshake
HTTP/1.1 101 Switching Protocols Upgrade: websocket Connection: Upgrade Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk= Sec-WebSocket-Protocol: chat
Wikipedia
![Page 26: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/26.jpg)
WebSockets
def msg(ws, msg): messages.extend(msg)
ws = websocket.WebSocketApp(‘ws://api.yo.com/', on_message=msg) ws.on_open = on_open ws.run_forever()
@sockets.route('/messages') def messages(ws): while True: message = wait_until_new_message() ws.send(message)
![Page 27: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/27.jpg)
![Page 28: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/28.jpg)
Bytes IN Bytes Out Total Bytes Time (seconds)
Polling 14640 13564 28204 159
Polling 2 7503 7636 15139 200
Long Polling 7503 7636 15139 120
Streaming 183 1549 1732 120
WebSockets 209 1605 1814 120
![Page 29: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/29.jpg)
0
3000
6000
9000
12000
15000
18000
21000
24000
27000
30000
Polling
Polling 2
Long Polling
Streaming
WebSocketsTotal Bytes Bytes Out Bytes IN
![Page 30: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/30.jpg)
Concurrency and the
C10k
![Page 31: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/31.jpg)
C1how to handle more than
10k connections simultaneously?
0k?
![Page 32: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/32.jpg)
concurrency is…
the ability of running in overlapping time periods,
not necessarily at the same time
![Page 33: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/33.jpg)
concurrency !=
parallelism
![Page 34: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/34.jpg)
processes➡ Preemptive scheduling by the OS ➡ Separate memory space ➡ No GIL related issues ➡ How to communicate them? ➡ They are really heavy
![Page 35: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/35.jpg)
threads➡ Preemptive scheduling by the OS ➡ Same memory space ➡ Faster and lighter than processes ➡ You may suffer the GIL ➡ What about synchronisation? ➡ Race conditions
![Page 36: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/36.jpg)
user threads➡ Same space address ➡ Lightest alternative ➡ No race-conditions ➡ You may suffer the GIL ➡ Very bad with CPU bound tasks
![Page 37: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/37.jpg)
GIL
![Page 38: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/38.jpg)
I/O methods
![Page 39: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/39.jpg)
again…how to handle more than
10k connections simultaneously?
![Page 40: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/40.jpg)
option 1
use 10k machines, one per client, VIP service
![Page 41: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/41.jpg)
option 2use blocking and
synchronous calls, one per client
![Page 42: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/42.jpg)
option 3non-blocking calls to start
I/O and then readiness notifications to know
when the socket is ready
![Page 43: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/43.jpg)
option 4asynchronous calls to
start I/O and completion notifications to know when they’ve finished
![Page 44: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/44.jpg)
but… in Python?
![Page 45: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/45.jpg)
twistedtornadogevent
![Page 46: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/46.jpg)
asyncio (aka tulip)
![Page 47: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/47.jpg)
asyncio➡ Single-threaded ➡ Async I/O ➡ Coroutines ➡ Multiplexes I/O events ➡ …
![Page 48: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/48.jpg)
COROUTINES
![Page 49: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/49.jpg)
websockets
asyncio
![Page 50: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/50.jpg)
demo
![Page 51: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/51.jpg)
![Page 52: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/52.jpg)
![Page 53: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/53.jpg)
![Page 54: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/54.jpg)
![Page 55: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/55.jpg)
XGame Of Life
![Page 56: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/56.jpg)
10K connections
![Page 57: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/57.jpg)
![Page 58: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/58.jpg)
questions?@igalarzab
![Page 59: Communication in Python and the C10k problem](https://reader033.vdocument.in/reader033/viewer/2022042522/55a4a7af1a28ab0a0c8b4581/html5/thumbnails/59.jpg)
thanks!