Talk at the Codemotion Spain 2014 about how to handle communication (polling, long polling, websockets, SSE), concurrency (processes, threads, coroutines, green threads) and the C10K problem in python
17. HTTP Streaming
➡ Better efficiency than (long) polling
➡ “Proxy friendly”
➡ Uni-directional
➡ You need to parse the data manually
18. HTTP Streaming
➡ Type 1: Connection Close
HTTP/1.1 200 OK
Content-Type: text/plain
Connection: close
Hello world
This a connection close response
19. 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
20. HTTP Streaming
@app.route("/messages")
def messages():
def content():
for sentence in wait_until_next_sentence():
yield sentence
return Response(content())
r = requests.get('http://yo.com/messages', stream=True)
for message in r.iter_lines():
messages.extend(message.decode(‘utf8’))
24. WebSockets
➡ Bidirectional
➡ Great efficiency
➡ A lot of implementations (JS API)
➡ Cross-domain
➡ Handshake to update from HTTP
➡ Complete different protocol
34. processes
➡ Preemptive scheduling by the OS
➡ Separate memory space
➡ No GIL related issues
➡ How to communicate them?
➡ They are really heavy
35. threads
➡ Preemptive scheduling by the OS
➡ Same memory space
➡ Faster and lighter than processes
➡ You may suffer the GIL
➡ What about synchronisation?
➡ Race conditions
36. user threads
➡ Same space address
➡ Lightest alternative
➡ No race-conditions
➡ You may suffer the GIL
➡ Very bad with CPU bound tasks