Top Banner
Cody Soyland Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13
24

Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Jun 11, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Cody SoylandAustin Web Python User Group - Feb 28, 2013

Concurrent Python

Thursday, February 28, 13

Page 2: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Thursday, February 28, 13

Page 3: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Getting started

• Notes on codysoyland.com

• Install gevent 1.0 (release candidate)

• Load my iPython Notebooks

Thursday, February 28, 13

Page 4: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

The “Real-time” Web

• Technologies that enable delivery of information to users the instant the servers know about it

• Interesting scaling challenges

• Lots of open connections

Thursday, February 28, 13

Page 5: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

High concurrency creates new challenges

• Non-blocking I/O

• Low resource overhead

• Distributed

Thursday, February 28, 13

Page 6: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Concurrency is not the same thing as Parallelism

• Concurrency is about the composition of independent processes

• Parallelism is about the simultaneous execution of independent processes

Thursday, February 28, 13

Page 7: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Building concurrent systems

• Processes (ie. CGI, mpm_prefork)

• Threads (ie. mpm_worker, most common)

• Non-blocking I/O

• Callbacks (CPS, Reactor Pattern)

• Coroutines

Thursday, February 28, 13

Page 8: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Threads• Pre-emptive scheduling (non-deterministic)

• Race conditions and locks/mutexes

• Memory overhead

• Readable, synchronous interface

• Guaranteed cooperation

Thursday, February 28, 13

Page 9: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Callbacks

• Call stack not preserved

• Simple things are intuitive

• Complex things become confusing

Thursday, February 28, 13

Page 10: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Coroutines

• Call stack preserved

• Synchronous API

• Benefits of threads without the non-determinism

Thursday, February 28, 13

Page 11: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Greenlet• True coroutines in Python

• Call switching from Stackless Python implemented as a C extension module

• Call stack slicing to preserve context

• Portions of the stack are copied to the heap and vice-versa

Thursday, February 28, 13

Page 12: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Gevent

• Expands upon greenlet to provide “green threads”

• Provides an event loop (libev with 1.0, libevent on current pre-1.0)

Thursday, February 28, 13

Page 13: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Green threads• Similar programming style to POSIX threads

• POSIX threads are pre-emptive

• Green threads are cooperative

• Many green threads can exist within a single POSIX thread

• Green threads are very lightweight

Thursday, February 28, 13

Page 14: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Crazy Fast!

http://nichol.as/benchmark-of-python-web-servers

Thursday, February 28, 13

Page 15: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

gevent.server>>> from gevent.server import StreamServer>>> def handle(socket, address):... socket.send('Your address is %s\n' % address[0])... >>> server = StreamServer(('127.0.0.1', 1234), handle)>>> server.serve_forever()

$ nc localhost 1234Your address is 127.0.0.1

Thursday, February 28, 13

Page 16: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

gevent.socket• Cooperative socket implementation

• When used with monkey patching, brings asynchronous network I/O to an abundance of third-party libraries:

• memcached

• redis-py

• boto

• requests

Thursday, February 28, 13

Page 17: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Wait, monkey patching?

• Yes!

• Uncooperative libraries are “patched” to cooperate with event loop

• Creates a large ecosystem of gevent-compatible libraries

Thursday, February 28, 13

Page 18: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

gevent.monkey• from gevent.monkey import patch_all; patch_all()

• Patches standard library:

• socket

• ssl

• os

• time

• select

• thread/threading

Thursday, February 28, 13

Page 19: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Deploying

• Use Gunicorn!

• In gunicorn.conf:

• worker_class = “gevent”

• That’s it! (well, almost)

Thursday, February 28, 13

Page 20: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Websockets

• gevent-websocket

• worker_class = geventwebsocket.gunicorn.workers.GeventWebSocketWorker

• gevent-socketio

• worker_class = socketio.sgunicorn.GeventSocketIOWorker

Thursday, February 28, 13

Page 21: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Green database library# gunicorn.conf

# Postgres

def post_fork(server, worker):

from psycogreen.gevent.psyco_gevent import patch_psycopg

patch_psycopg()

# MySQL

def post_fork(server, worker):

import pymysql

pymysql.install_as_MySQLdb()

Thursday, February 28, 13

Page 22: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Scaling• Messaging (backend)

• ZeroMQ: zmq.green

• Redis (monkey-patch compatible)

• Load balancing (frontend)

• HAProxy

• Varnish

• Nginx 1.3+

Thursday, February 28, 13

Page 23: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Further Resources• gevent For the Working Python Developer

• http://sdiehl.github.com/gevent-tutorial/

• A Curious on Coroutines and Concurrency - Beazley

• www.dabeaz.com/coroutines/Coroutines.pdf

• Concurrency is not Parallelism - Rob Pike

• http://vimeo.com/49718712

Thursday, February 28, 13

Page 24: Concurrent Python - Cody Soylandmedia.codysoyland.com/pdf/concurrent-python.pdf · Austin Web Python User Group - Feb 28, 2013 Concurrent Python Thursday, February 28, 13. Thursday,

Thanks! Questions?

• codysoyland.com

[email protected]

• twitter.com/codysoyland

• github.com/codysoyland

Thursday, February 28, 13