2. Concurrency
handling a number of things at the same time
Examples: (Incoming) WebServers, Database Servers
(Outgoing) SSH Mux
3. SSH Mux
●
Execute a command on a remote
SSH server
●
Handle concurrent SSH Clients
●
Command execution time varies
from seconds to days
●
Command execution happens on
remote servers, SSH mux is I/O
bound
4. SSH Client
1. Init session
2. Authenticate
3. Get a channel
4. Issue command
5. Read output
5. Need Concurrency?
●
Process blocks on read()
●
No new connections can be inititated
●
Need ability to handle multiple clients at the same
time
6. Multiprocessing
●
One process is the master
●
Master can spawn workers
●
Each worker handles one request at a time
●
Pre-forked pool of workers
10. Multiprocessing yay
●
Easy to get started
●
OS guaranteed process isolation & fairness
●
Covers up for misbehaving workers
●
Add more concurrency by adding more workers
●
Convenient when numbers are smaller numbers
11. Multiprocessing nay
●
Concurrency limited by number of processes
●
Memory heavy
●
Implicit scheduling
●
Synchronization is not trivial
12. More Concurrency?
●
Command execution is happening on remote servers, we
are mostly blocked on I/O
●
Handle multiple I/O in a single process?
13. Gevent
gevent is a coroutine-based Python networking library that
uses greenlet to provide a high-level synchronous API on
top of the libevent event loop.
14. Greenlets
●
Lightweight 'threads' - not OS threads
●
Explicit scheduling - Cooperative
●
Minimal stack
●
Application decides execution flow
●
Easy to synchronize/ Avoid locks
●
All run inside one process
15. Libevent
●
Use fastest mechanism to poll (portable)
●
Fast Event loop
●
In Gevent, event loop runs in a greenlet (event hub)
●
Instead of blocking, greenlets switch to event hub
●
It's all done behind the scene
32. Gevent yay!
●
Untwist – write linear non blocking code
●
Explicit scheduling, dictate the execution flow
●
Timeouts
●
Events, AsyncResults for Synchronization
●
gevent.wsgi
●
Pre-spawned pool of greenlets
33. Gevent beware of
●
No multicore support
●
Not great for CPU bound applications
●
Third party libs must be green (non blocking)
●
Misbehaving workers can be lethal
●
No fairness when it comes to scheduling
34. Take Away
●
Gevent lets you write asynchronous code in a
synchronous manner
●
No multicore support, still need multiprocessing
●
Not so great for CPU bound applications
●
Split your application into CPU bound and IO bound
parts
●
Be willing to contribute patches
●
Code available at
git@github.com:aaloksood/pyexamples.git