communication in python and the c10k problem

59
Jose Ignacio Galarza @igalarzab Communication in Python and the C10k problem

Upload: jose-ignacio-galarza

Post on 14-Jul-2015

350 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Communication in Python and the C10k problem

Jose Ignacio Galarza @igalarzab

Communication in Python and the C10k problem

Page 2: Communication in Python and the C10k problem

MANOLOOOOO!!!!

Page 3: Communication in Python and the C10k problem
Page 4: Communication in Python and the C10k problem

select the best method to

communicate

Page 5: Communication in Python and the C10k problem

select the best method to scale

Page 6: Communication in Python and the C10k problem
Page 7: Communication in Python and the C10k problem

Index✤ From polling… to pushing ✤ Concurrency ✤ C10k ✤ Asynchronous I/O

Page 8: Communication in Python and the C10k problem

PYTHON 3

PYTHON 3 EVERYWHERE

Page 9: Communication in Python and the C10k problem

From polling to pushing

Page 10: Communication in Python and the C10k problem

polling

Client Server

Page 11: Communication in Python and the C10k problem

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

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

long polling

ServerClient

Page 14: Communication in Python and the C10k problem

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

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

HTTP Streaming

ServerClient

Page 17: Communication in Python and the C10k problem

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

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

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

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

SSE

ServerClient

Page 22: Communication in Python and the C10k problem

SSE➡ Well known protocol (HTTP) ➡ Good efficiency ➡ JS API ➡ Few client implementations ➡ Uni-directional

Page 23: Communication in Python and the C10k problem

WebSockets

ServerClient

Page 24: Communication in Python and the C10k problem

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

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

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
Page 28: Communication in Python and the C10k problem

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

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

Concurrency and the

C10k

Page 31: Communication in Python and the C10k problem

C1how to handle more than

10k connections simultaneously?

0k?

Page 32: Communication in Python and the C10k problem

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

concurrency !=

parallelism

Page 34: Communication in Python and the C10k problem

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

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

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

GIL

Page 38: Communication in Python and the C10k problem

I/O methods

Page 39: Communication in Python and the C10k problem

again…how to handle more than

10k connections simultaneously?

Page 40: Communication in Python and the C10k problem

option 1

use 10k machines, one per client, VIP service

Page 41: Communication in Python and the C10k problem

option 2use blocking and

synchronous calls, one per client

Page 42: Communication in Python and the C10k problem

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

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

but… in Python?

Page 45: Communication in Python and the C10k problem

twistedtornadogevent

Page 46: Communication in Python and the C10k problem

asyncio (aka tulip)

Page 47: Communication in Python and the C10k problem

asyncio➡ Single-threaded ➡ Async I/O ➡ Coroutines ➡ Multiplexes I/O events ➡ …

Page 48: Communication in Python and the C10k problem

COROUTINES

Page 49: Communication in Python and the C10k problem

websockets

asyncio

Page 50: Communication in Python and the C10k problem

demo

Page 51: Communication in Python and the C10k problem
Page 52: Communication in Python and the C10k problem
Page 53: Communication in Python and the C10k problem
Page 54: Communication in Python and the C10k problem
Page 55: Communication in Python and the C10k problem

XGame Of Life

Page 56: Communication in Python and the C10k problem

10K connections

Page 57: Communication in Python and the C10k problem
Page 58: Communication in Python and the C10k problem

questions?@igalarzab

Page 59: Communication in Python and the C10k problem

thanks!