SlideShare una empresa de Scribd logo
1 de 39
Concurrency
 & Gevent
Scaling Real World Applications
Concurrency
handling a number of things at the same time




Examples: (Incoming) WebServers, Database Servers
         (Outgoing) SSH Mux
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
SSH Client


1. Init session

2. Authenticate

3. Get a channel

4. Issue command

5. Read output
Need Concurrency?
●
    Process blocks on read()
●
    No new connections can be inititated
●
    Need ability to handle multiple clients at the same
    time
Multiprocessing
●
    One process is the master
●
    Master can spawn workers
●
    Each worker handles one request at a time
●
    Pre-forked pool of workers
Concurrent SSH Clients
SSH Mux Memory Usage

                                              ssh mux memory usage

               600


               500


               400
 Memory (MB)




               300                                                                     Processes


               200


               100


                0
                     0   5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
                                            # Concurrent Reqs
SSH Mux Performace

                                     ssh mux performance sheet

          160

          140

          120

          100
Time(s)




          80                                                                           Processes

          60

          40

          20

           0
                20   40   80   150   200     400     800   1200   1500   1800   2100
                                     # Concurrent Reqs
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
Multiprocessing nay
●
    Concurrency limited by number of processes
●
    Memory heavy
●
    Implicit scheduling
●
    Synchronization is not trivial
More Concurrency?

●
    Command execution is happening on remote servers, we
    are mostly blocked on I/O




●
    Handle multiple I/O in a single process?
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.
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
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
Monkey Patching

Monkey patching
Modifies behaviour of blocking calls such as select, sleep to
non-blocking
Patches the python standard socket library
Gevent
●   Greenlet 1 is running
●   Greenlet 2 and 3 are ready
Gevent
●
    Greenlet 1 has to wait for read
●
    Greenlet 1 switches to Event hub
Gevent
●
    Event hub switches to Greenlet 3
Gevent
●   Greenlet 2 runs
Gevent
●
    Greenlet 2 wants to sleep
●
    Greenlet 2 switches to Event hub
Gevent
●
    Greenlet 1 data has come, moved to ready state
●
    Eventhub switches to Greenlet 3
Gevent
●   Greenlet 3 runs
Gevent
●   When Greenlet 1 resumes, its from next instruction
●   It's as if it were a blocking call
Green SSH Client


1. Init session

2. Authenticate

3. Get a channel

4. Issue command

5. Read output
A closer look
Going Concurrent




 Use pre-forked processes to use all cores
Memory usage
                                               ssh mux memory usage

             45

             40

             35

             30
Memory(MB)




             25
                                                                                    Gevent+Processes
             20

             15

             10

             5

             0
                  0   5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100

                                          # Concurrent Reqs
SSH Mux Performace
                                           ssh mux performace chart

           70


           60


           50


           40
 Time(s)




                                                                                     Gevent+Processes
           30


           20


           10


           0
                20   40   80   150   200     400   800   1200   1500   1800   2100
                                     # Concurrent Reqs
SSH Mux memory usage

                                              ssh mux memory usage

                600


                500


                400
   Memory(MB)




                                                                                        Processes
                300
                                                                                        Gevent

                200


                100


                 0
                      0   5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
                                             # Concurrent Reqs
SSH Mux Performance
                                        ssh mux performance sheet

          160

          140

          120

          100
                                                                                       Processes
Time(s)




          80                                                                           Gevent+Processes

          60

          40

          20

           0
                20   40   80   150   200     400     800   1200   1500   1800   2100

                                     # Concurrent Reqs
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
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
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
Thank you




            That's all folks!
Countdown Timer
●
    Count down from 200000000
●
    Split work among workers
Threads

                                 Multithreading wonder

            25


            20


            15
                                                                      1 Core
  Time(s)




                                                                      4 cores
            10


            5


            0
                 1   2   3   4     5           6   7     8   9   10
                                   # Workers
One core


                                    Execution time One Core

             14.5
              14
             13.5
              13                                                           Processes
                                                                           1 Core
  Time (s)




             12.5
                                                                           Gevent_1
              12                                                           Gevent_4
             11.5
              11
             10.5
                    1   2   3   4       5         6   7       8   9   10
                                      # Workers
Four cores


                                  Execution time 4 cores

             25


             20

                                                                        Process
             15
                                                                        Threads
   Time(s)




                                                                        Gevent_1
             10
                                                                        Gevent_4

             5


             0
                  1   2   3   4     5          6   7       8   9   10
                                   # Workers

Más contenido relacionado

Similar a Scaling real world applications using gevent

Usenix LISA 2012 - Choosing a Proxy
Usenix LISA 2012 - Choosing a ProxyUsenix LISA 2012 - Choosing a Proxy
Usenix LISA 2012 - Choosing a ProxyLeif Hedstrom
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsMarcus Frödin
 
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance TuningPyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance TuningGraham Dumpleton
 
Joomla! Day Deutschland 2012 - Active Security
Joomla! Day Deutschland 2012 - Active SecurityJoomla! Day Deutschland 2012 - Active Security
Joomla! Day Deutschland 2012 - Active SecurityNicholas Dionysopoulos
 
Microservices with Micronaut
Microservices with MicronautMicroservices with Micronaut
Microservices with MicronautQAware GmbH
 
Puppet Availability and Performance at 100K Nodes - PuppetConf 2014
Puppet Availability and Performance at 100K Nodes - PuppetConf 2014Puppet Availability and Performance at 100K Nodes - PuppetConf 2014
Puppet Availability and Performance at 100K Nodes - PuppetConf 2014Puppet
 
Analyze Virtual Machine Overhead Compared to Bare Metal with Tracing
Analyze Virtual Machine Overhead Compared to Bare Metal with TracingAnalyze Virtual Machine Overhead Compared to Bare Metal with Tracing
Analyze Virtual Machine Overhead Compared to Bare Metal with TracingScyllaDB
 
Analyzing OS X Systems Performance with the USE Method
Analyzing OS X Systems Performance with the USE MethodAnalyzing OS X Systems Performance with the USE Method
Analyzing OS X Systems Performance with the USE MethodBrendan Gregg
 
ずばり動く!kumofs と ずばり動かないケース
ずばり動く!kumofs と ずばり動かないケースずばり動く!kumofs と ずばり動かないケース
ずばり動く!kumofs と ずばり動かないケースSadayuki Furuhashi
 
OSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialOSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialTom Croucher
 
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMUSFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMULinaro
 
Lessons Learnt in 2009
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009pratiknaik
 
XPDS16: Xen Live Patching - Updating Xen Without Rebooting - Konrad Wilk, Ora...
XPDS16: Xen Live Patching - Updating Xen Without Rebooting - Konrad Wilk, Ora...XPDS16: Xen Live Patching - Updating Xen Without Rebooting - Konrad Wilk, Ora...
XPDS16: Xen Live Patching - Updating Xen Without Rebooting - Konrad Wilk, Ora...The Linux Foundation
 
Scaling sql server 2014 parallel insert
Scaling sql server 2014 parallel insertScaling sql server 2014 parallel insert
Scaling sql server 2014 parallel insertChris Adkin
 
分散Key-valueストアkumofsの思想と設計
分散Key-valueストアkumofsの思想と設計分散Key-valueストアkumofsの思想と設計
分散Key-valueストアkumofsの思想と設計Sadayuki Furuhashi
 
[오픈소스컨설팅] 프로메테우스 모니터링 살펴보고 구성하기
[오픈소스컨설팅] 프로메테우스 모니터링 살펴보고 구성하기[오픈소스컨설팅] 프로메테우스 모니터링 살펴보고 구성하기
[오픈소스컨설팅] 프로메테우스 모니터링 살펴보고 구성하기Ji-Woong Choi
 

Similar a Scaling real world applications using gevent (20)

Usenix LISA 2012 - Choosing a Proxy
Usenix LISA 2012 - Choosing a ProxyUsenix LISA 2012 - Choosing a Proxy
Usenix LISA 2012 - Choosing a Proxy
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.js
 
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance TuningPyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
 
Getput suite
Getput suiteGetput suite
Getput suite
 
Joomla! Day Deutschland 2012 - Active Security
Joomla! Day Deutschland 2012 - Active SecurityJoomla! Day Deutschland 2012 - Active Security
Joomla! Day Deutschland 2012 - Active Security
 
Microservices with Micronaut
Microservices with MicronautMicroservices with Micronaut
Microservices with Micronaut
 
Puppet Availability and Performance at 100K Nodes - PuppetConf 2014
Puppet Availability and Performance at 100K Nodes - PuppetConf 2014Puppet Availability and Performance at 100K Nodes - PuppetConf 2014
Puppet Availability and Performance at 100K Nodes - PuppetConf 2014
 
Rails Performance
Rails PerformanceRails Performance
Rails Performance
 
Analyze Virtual Machine Overhead Compared to Bare Metal with Tracing
Analyze Virtual Machine Overhead Compared to Bare Metal with TracingAnalyze Virtual Machine Overhead Compared to Bare Metal with Tracing
Analyze Virtual Machine Overhead Compared to Bare Metal with Tracing
 
Analyzing OS X Systems Performance with the USE Method
Analyzing OS X Systems Performance with the USE MethodAnalyzing OS X Systems Performance with the USE Method
Analyzing OS X Systems Performance with the USE Method
 
ずばり動く!kumofs と ずばり動かないケース
ずばり動く!kumofs と ずばり動かないケースずばり動く!kumofs と ずばり動かないケース
ずばり動く!kumofs と ずばり動かないケース
 
OSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js TutorialOSCON 2011 - Node.js Tutorial
OSCON 2011 - Node.js Tutorial
 
RubyKaigi 2014: ServerEngine
RubyKaigi 2014: ServerEngineRubyKaigi 2014: ServerEngine
RubyKaigi 2014: ServerEngine
 
Devignition 2011
Devignition 2011Devignition 2011
Devignition 2011
 
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMUSFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
SFO15-202: Towards Multi-Threaded Tiny Code Generator (TCG) in QEMU
 
Lessons Learnt in 2009
Lessons Learnt in 2009Lessons Learnt in 2009
Lessons Learnt in 2009
 
XPDS16: Xen Live Patching - Updating Xen Without Rebooting - Konrad Wilk, Ora...
XPDS16: Xen Live Patching - Updating Xen Without Rebooting - Konrad Wilk, Ora...XPDS16: Xen Live Patching - Updating Xen Without Rebooting - Konrad Wilk, Ora...
XPDS16: Xen Live Patching - Updating Xen Without Rebooting - Konrad Wilk, Ora...
 
Scaling sql server 2014 parallel insert
Scaling sql server 2014 parallel insertScaling sql server 2014 parallel insert
Scaling sql server 2014 parallel insert
 
分散Key-valueストアkumofsの思想と設計
分散Key-valueストアkumofsの思想と設計分散Key-valueストアkumofsの思想と設計
分散Key-valueストアkumofsの思想と設計
 
[오픈소스컨설팅] 프로메테우스 모니터링 살펴보고 구성하기
[오픈소스컨설팅] 프로메테우스 모니터링 살펴보고 구성하기[오픈소스컨설팅] 프로메테우스 모니터링 살펴보고 구성하기
[오픈소스컨설팅] 프로메테우스 모니터링 살펴보고 구성하기
 

Último

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dashnarutouzumaki53779
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 

Último (20)

Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dash
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 

Scaling real world applications using gevent

  • 1. Concurrency & Gevent Scaling Real World Applications
  • 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
  • 8. SSH Mux Memory Usage ssh mux memory usage 600 500 400 Memory (MB) 300 Processes 200 100 0 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 # Concurrent Reqs
  • 9. SSH Mux Performace ssh mux performance sheet 160 140 120 100 Time(s) 80 Processes 60 40 20 0 20 40 80 150 200 400 800 1200 1500 1800 2100 # Concurrent Reqs
  • 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
  • 16. Monkey Patching Monkey patching Modifies behaviour of blocking calls such as select, sleep to non-blocking Patches the python standard socket library
  • 17. Gevent ● Greenlet 1 is running ● Greenlet 2 and 3 are ready
  • 18. Gevent ● Greenlet 1 has to wait for read ● Greenlet 1 switches to Event hub
  • 19. Gevent ● Event hub switches to Greenlet 3
  • 20. Gevent ● Greenlet 2 runs
  • 21. Gevent ● Greenlet 2 wants to sleep ● Greenlet 2 switches to Event hub
  • 22. Gevent ● Greenlet 1 data has come, moved to ready state ● Eventhub switches to Greenlet 3
  • 23. Gevent ● Greenlet 3 runs
  • 24. Gevent ● When Greenlet 1 resumes, its from next instruction ● It's as if it were a blocking call
  • 25. Green SSH Client 1. Init session 2. Authenticate 3. Get a channel 4. Issue command 5. Read output
  • 27. Going Concurrent Use pre-forked processes to use all cores
  • 28. Memory usage ssh mux memory usage 45 40 35 30 Memory(MB) 25 Gevent+Processes 20 15 10 5 0 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 # Concurrent Reqs
  • 29. SSH Mux Performace ssh mux performace chart 70 60 50 40 Time(s) Gevent+Processes 30 20 10 0 20 40 80 150 200 400 800 1200 1500 1800 2100 # Concurrent Reqs
  • 30. SSH Mux memory usage ssh mux memory usage 600 500 400 Memory(MB) Processes 300 Gevent 200 100 0 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 # Concurrent Reqs
  • 31. SSH Mux Performance ssh mux performance sheet 160 140 120 100 Processes Time(s) 80 Gevent+Processes 60 40 20 0 20 40 80 150 200 400 800 1200 1500 1800 2100 # Concurrent Reqs
  • 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
  • 35. Thank you That's all folks!
  • 36. Countdown Timer ● Count down from 200000000 ● Split work among workers
  • 37. Threads Multithreading wonder 25 20 15 1 Core Time(s) 4 cores 10 5 0 1 2 3 4 5 6 7 8 9 10 # Workers
  • 38. One core Execution time One Core 14.5 14 13.5 13 Processes 1 Core Time (s) 12.5 Gevent_1 12 Gevent_4 11.5 11 10.5 1 2 3 4 5 6 7 8 9 10 # Workers
  • 39. Four cores Execution time 4 cores 25 20 Process 15 Threads Time(s) Gevent_1 10 Gevent_4 5 0 1 2 3 4 5 6 7 8 9 10 # Workers