SlideShare ist ein Scribd-Unternehmen logo
1 von 77
Downloaden Sie, um offline zu lesen
ASYNCHRONOUS
                           PROGRAMMING IN
                               PYTHON
                               With Rocks In




Wednesday, 31 August, 11
Not really covering Tornado




Wednesday, 31 August, 11
Introduction to Asynchronous




Wednesday, 31 August, 11
Still intermediate




Wednesday, 31 August, 11
Just mostly Twisted




Wednesday, 31 August, 11
Just mostly Twisted
                           ... with some Tornado.



Wednesday, 31 August, 11
Introductions




           •   Hi, I'm Aurynn

           •   This is a hedgehog




Wednesday, 31 August, 11
I’m more like a Magpie




           •   Shiny things are SO COOL

           •   I could talk about how
               shiny they are until




                            http://www.flickr.com/photos/cmg2011/5147250751/

Wednesday, 31 August, 11
Look! A particularly shiny thing!




Wednesday, 31 August, 11
Buzzword Bingo


           •   Much ado about Node.js

           •   Event Driven!

           •   Tornado, from Facebook!

           •   Event loops!

           •   “Web Scale!”



Wednesday, 31 August, 11
Lots of Chatter




           •   Without lots of research, it’s
               just kind of noise




                           http://en.wikipedia.org/wiki/File:Carl_Friedrich_Gauss.jpg

Wednesday, 31 August, 11
Past the buzzwords, useful ideas




Wednesday, 31 August, 11
Asynchronous programming, or




Wednesday, 31 August, 11
Doing more than one thing at once




Wednesday, 31 August, 11
... sort of.




Wednesday, 31 August, 11
And it’s the sort of that is
             important




Wednesday, 31 August, 11
A TERRIBLY BRIEF,
                   PROBABLY INACCURATE
                         HISTORY




Wednesday, 31 August, 11
Threading, you’ve heard of it


           •   Really common

           •   Java, .NET, even some
               Python

           •   Super awesome! Shared
               memory, shared scopes, fun
               all around..



Wednesday, 31 August, 11
Surprisingly good, until it isn’t


           •   Very difficult to access
               shared state safely

           •   Race conditions

           •   Even experts have a hard
               time of it

           •   Generally hard to do right



Wednesday, 31 August, 11
The kernel cares about when things happen, not you.


                               Multiprocessing        No problems with locks or race conditions, since you
                                                      don’t have a consistent memory region

                                                      Fork() makes life so easy!

                                                      the MP model even makes multiple-systems a viable
                                                      approach: it’s pretty trivial to SSH into another
                                                      computer and run a program, or a batch of programs.




           •   Let the kernel care!

           •   Fairly easy to write MP
               code on unix-likes

           •   Can even go multi-system



                            http://www.flickr.com/photos/epw/2876377014/

Wednesday, 31 August, 11
Hard to share Data


           •   It’s not easy to send data
               between processes

           •   Parsing stdout, or trying to
               get a shmem
               implementation working.

           •   import multiprocessing can
               be.. quirky.



Wednesday, 31 August, 11
Asynchronous!


           •   Like threading, everything
               is in a single process

           •   All my Variables, All the
               Time

           •   No race conditions (mostly)

           •   Guarantee of no
               concurrent execution

                           http://www.flickr.com/photos/rachelpasch/3754315974/

Wednesday, 31 August, 11
Not all Unicorns and Rainbows
                                          Just like MP and threads, event loops have their own caveats and major
                                          constraints.

                                          Your code can’t run indefinitely, and the longer it runs, the longer your process
                                          stalls.

                                          Like threading, it’s still Not Easy to get your head around how to write
                                          asynchronous code, and this is something else we’ll go into in a bit more detail.

                                          Single mistakes, not catching your errors in The Approved Way? You can very
                                          easily trash your entire program and cause yourself to hang. Why does this
                                          happen? It comes back to the first point of You Need to Let Go.
           •   You have to Let Go

           •   Bending your mind to the
               Asynchronous Way is still
               hard

           •   A single mistake can hang

           •   Probably going to be slower.

                           http://www.flickr.com/photos/digitalpapercuts/5737975961

Wednesday, 31 August, 11
SO REALLY, WHAT IS ASYNC?
                           So, I’ve made some broad generalizations about event
                           loops, and the caveats they bring to the table.
                           Let’s go into some more detail about what they do and
                           are, and how those caveats actually work, and look at
                           some code to really show how to work in the
                           Asynchronous Way.




Wednesday, 31 August, 11
so, to get this far, we haven’t really
                                     answered the first question:
                                      what *is* an event loop?




                What is an event loop?




Wednesday, 31 August, 11
What is an event loop?                         At its heart, an event loop is just a long-running
                                                          while loop, iterating over a set of callbacks, or
                                                          events to be run in the future.

                                                          When an event gets triggered, often in the form
                                                          of a socket message, or the completion of
                                                          another function, or a timeout firing.




           •   A long-running while loop

           •   When an event triggers, the loop catches this fact

           •   Events are pretty generic




Wednesday, 31 August, 11
Then what?




Wednesday, 31 August, 11
Then what?                          When you added the event you care about, you
                                               also added a callback. A callback is simply a
                                               Python function that gets run with the results
                                               of the event.
                                               The return value of a function, or the data
                                               coming off your socket, or whatever is what
                                               this function gets passed.

                                               The great part is that this function definition is
                                               allowed to be *any callable* object in Python. A
                                               class with .__call__, a function, a bound method
                                               on an object, whatever scope you like, it has.




           •   Let my code know!

           •   This code can be any callable




Wednesday, 31 August, 11
But once it’s in your code..




Wednesday, 31 August, 11
You have to Let Go              As you’ve probably figured out, what happens in
                                           an event system is analogous to co-operative
                                           multitasking.
                                           When an event fires and your callback gets run,
                                           what happens?
                                           Since it’s a standard method call,




                           Event Loop




                                        Your Code



Wednesday, 31 August, 11
You have to Let Go              control is handed over to your method, and
                                           doesn’t return to the event loop UNTIL YOU
                                           RETURN.




                           Event Loop




                                        Your Code



Wednesday, 31 August, 11
We could be here a while...            control is handed over to your method, and
                                                  doesn’t return to the event loop UNTIL YOU
                                                  RETURN.

                                                  So let’s say your particular callback takes, oh
                                                  second to do its thing, as it’s a particularly
                                                  computationally intensive, your entire event
                                                  loop is unable to do anything else.




                            Let’s compute Pi to
                           a BILLION decimal
                                   places!




                                                        Your Code

Wednesday, 31 August, 11
Not just silly maths, either       This happens no matter what your code does,
                                              it silly math or a web site reaching out to
                                              MySQL for data, or going to disk to open a file
                                              iterating over a long array, or even waiting on
                                              the user to do something.

                                              As long as your code hasn’t returned, your ent
                                              program has STALLED.




                           I need some data
                             from MySQL.




                                                    Your Code

Wednesday, 31 August, 11
You have STALLED.


                            http://www.flickr.com/photos/neilwill/5023734329/

Wednesday, 31 August, 11
It should be fairly obvious that this is bad, and why it’s bad.

                                      To use the example of a hypothetical website, if you’re stalled
                                      waiting for the database, you can’t accept new connections,
                                      and you can’t even give an indication why. Your site will
                                      *appear* to perform slowly.




                Is that really bad?




Wednesday, 31 August, 11
Solving this isn’t easy, and requires adjusting your mental
                                  model on how programs flow.

                                  In Twisted, programs have to be written with the idea that a
                                  method call won’t return the results you expect, but instead
                                  an object that will tell a function what your data is.




                Asynchronous code is harder




Wednesday, 31 August, 11
Asynchronous code is harder

                                               For instance, x = y() won’t do what you expect.

                                               How can it, when you’re not actually




           •   x = y() doesn’t work anymore.




Wednesday, 31 August, 11
Asynchronous code is harder

                                               In this model, you end up with very tiny
                                               functions that perform very small, discrete
                                               amounts of work, before releasing control back
                                               to the event loop.

                                               In order for these very tiny functions to be
                                               useful, we have to keep tight control over our
                                               scope, and an easy way to do that is by using
                                               closures.

           •   x = y() doesn’t work anymore.

           •   Requires very tiny functions




Wednesday, 31 August, 11
This is what Twisted does   As you can see here, we’ve expanded our row
                                       processor into its own function, as well as adding
                                       an error handler to the t wisted Deferred.




Wednesday, 31 August, 11
The first way in which asynchronous code can be written is
                                   through the use of closures.

                                   A closure is a funky sort of internal, anonymous function
                                   that “closes over” the scope of the function it’s defined in.

                                   This can be very powerful, as the closure effectively
                                   “resumes” back in the middle of the original function, can
                                   update state, and generally do useful things.




                Wait, wait, what just happened?




Wednesday, 31 August, 11
What that was was a *deferred*, basically the core
                                   response that you’ll get out of an API in Twisted.

                                   What a deferred is, is an indicator that something is going to
                                   happen *later on*, as opposed to right now.

                                   This comes back to the core ideal of having to let go, and let’s
                                   go back to the code to explain further




                Deferred, the Core of Twisted




Wednesday, 31 August, 11
The Core of Twisted




           •   Most APIs built on Twisted return Deferreds

           •   Almost always involve user code




Wednesday, 31 August, 11
What that was was a *deferred*, basically the core
                                   response that you’ll get out of an API in Twisted.

                                   What a deferred is, is an indicator that something is going to
                                   happen *later on*, as opposed to right now.

                                   This comes back to the core ideal of having to let go, and let’s
                                   go back to the code to explain further




                But what is a Deferred?




Wednesday, 31 August, 11
But what is it?   These are the basic features that you probably care
                             about in a deferred;

                             Add callbacks and errbacks, which we’ve already
                             covered a bit of,

                             and these new methods, .callback and .errback.




Wednesday, 31 August, 11
For instance, x = y() won’t do what you expect.


           Segue Power!                                 How can it, when you’re not actually




           •   .callback starts the callback chain

           •   .errback causes the callback chain to explode and die
               messily




Wednesday, 31 August, 11
What that was was a *deferred*, basically the core
                                      response that you’ll get out of an API in Twisted.

                                      What a deferred is, is an indicator that something is going to
                                      happen *later on*, as opposed to right now.

                                      This comes back to the core ideal of having to let go, and let’s
                                      go back to the code to explain further




                errback is structurally identical to
                callbacks




Wednesday, 31 August, 11
Let’s look at this again




Wednesday, 31 August, 11
What that was was a *deferred*, basically the core
                                   response that you’ll get out of an API in Twisted.

                                   What a deferred is, is an indicator that something is going to
                                   happen *later on*, as opposed to right now.

                                   This comes back to the core ideal of having to let go, and let’s
                                   go back to the code to explain further




                What’s the key here?




Wednesday, 31 August, 11
What that was was a *deferred*, basically the core
                                    response that you’ll get out of an API in Twisted.

                                    What a deferred is, is an indicator that something is going to
                                    happen *later on*, as opposed to right now.

                                    This comes back to the core ideal of having to let go, and let’s
                                    go back to the code to explain further




                It doesn’t happen right away.




Wednesday, 31 August, 11
Synchronous Example




Wednesday, 31 August, 11
And again, for comparison




Wednesday, 31 August, 11
What that was was a *deferred*, basically the core
                              response that you’ll get out of an API in Twisted.

                              What a deferred is, is an indicator that something is going to
                              happen *later on*, as opposed to right now.

                              This comes back to the core ideal of having to let go, and let’s
                              go back to the code to explain further




                Composition




Wednesday, 31 August, 11
What that was was a *deferred*, basically the core
                                   response that you’ll get out of an API in Twisted.

                                    What a deferred is, is an indicator that something is going to
                                    happen *later on*, as opposed to right now.

                                    This comes back to the core ideal of having to let go, and let’s
                                    go back to the code to explain further




                Or, chaining callbacks




Wednesday, 31 August, 11
Wha?




Wednesday, 31 August, 11
What that was was a *deferred*, basically the core
                                  response that you’ll get out of an API in Twisted.

                                  What a deferred is, is an indicator that something is going to
                                  happen *later on*, as opposed to right now.

                                  This comes back to the core ideal of having to let go, and let’s
                                  go back to the code to explain further




                Synchronous callback chain




Wednesday, 31 August, 11
ASYNCHRONOUS != FASTER
                           The final caveat on our List of Asynchronous Problems
                           is the idea I’ve run into that asynchronous code is, by
                           the very fact of running on an evented IO ser ver, it
                           will be faster.

                           This idea is all sorts of wrong.
                           Synchronous code that simply runs inside of a event-
                           driven IO system like Twisted or Tornado is naturally
                           going to be slower than the same code running
                           standalone.




Wednesday, 31 August, 11
Synchronous code that simply runs inside of a event-driven
                                   IO system like Twisted or Tornado is naturally going to be
                                   slower than the same code running standalone.




                The event loop is overhead




Wednesday, 31 August, 11
And, as you’ve already seen on how to structure
                                    asynchronous programs, effectively useless, unless you take
                                    the time to program to take advantage of an asynchronous
                                    event loop.




                The event loop is overhead...

                ..and without proper coding, useless




Wednesday, 31 August, 11
The question you all want to ask




Wednesday, 31 August, 11
WHY BOTHER?
                            Why spend extra time doing it the Hard Way, the way
                            where you are required to do more work and write code
                            in completely new ways?




Wednesday, 31 August, 11
The real advantage, the real power of asynchronous
                                     programming is the level of scale to which you can go.

                                     No threads means no thread overhead, and no complexity of
                                     maintaining locks and trying to share state.

                                     Nginx, well-regarded as one of the fastest webser vers
                                     around, is entirely built around asynchronous programming.




                Scales beautifully




Wednesday, 31 August, 11
Once you really “get it”, the entire idea starts seeming
                                   terribly elegant and worthwhile, and you start looking for
                                   how to process code asynchronously in all aspects of your
                                   programming.




                Terribly elegant




Wednesday, 31 August, 11
You also end up in a position where you’re writing far more
                                  reusable code.
                                  Why? Well, you need to have these functions which run as
                                  callbacks, and as we’ll go into in a little bit, those same
                                  callbacks can be chained together. There’s very little point in
                                  rewriting code all the time to




                More re-usable code




Wednesday, 31 August, 11
What do I mean by this? Your code is often going to be
                                   waiting for other servers - webser vers, database ser vers,
                                   net work, file, Everything.

                                   So the example I have the




                Closer mapping to reality




Wednesday, 31 August, 11
LITTLE BITS OF TORNADOS
                           Since I’ve spent most of my time so far talking
                           about Twisted as opposed to the other “major”
                           asynchronous platform, I’d like to devote a little bit
                           of time to Tornado.




Wednesday, 31 August, 11
Tornado, while it does have an internal IO loop, and libraries
                                 *do* use it standalone, the vast majority of examples you’ll
                                 run across take the idea of it being a web framework akin to
                                 Pylons or Pyramid or Bottle.




                Event loop + web framework




Wednesday, 31 August, 11
Callbacks are inline




Wednesday, 31 August, 11
Not as wide library support




Wednesday, 31 August, 11
Lacks a deferred metaphor




Wednesday, 31 August, 11
Tornado, while it does have an internal IO loop, and libraries
                                    *do* use it standalone, the vast majority of examples you’ll
                                    run across take the idea of it being a web framework akin to
                                    Pylons or Pyramid or Bottle.




                .add_callback(my_function)




Wednesday, 31 August, 11
my_function has to handle both




Wednesday, 31 August, 11
my_function( response, error )




Wednesday, 31 August, 11
So why Tornado over Twisted?




Wednesday, 31 August, 11
Speed.
     http://programmingzen.com/2009/09/13/benchmarking-tornado-vs-twisted-web-vs-tornado-on-twisted/

Wednesday, 31 August, 11
SO, THAT’S IT.




Wednesday, 31 August, 11
ANY QUESTIONS?




Wednesday, 31 August, 11
THANKS!




Wednesday, 31 August, 11

Weitere ähnliche Inhalte

Andere mochten auch

用Tornado开发RESTful API运用
用Tornado开发RESTful API运用用Tornado开发RESTful API运用
用Tornado开发RESTful API运用Felinx Lee
 
Introdução a linguagem Go
Introdução a linguagem GoIntrodução a linguagem Go
Introdução a linguagem GoAllisson Azevedo
 
Introduction to Tornado - TienNA
Introduction to Tornado - TienNAIntroduction to Tornado - TienNA
Introduction to Tornado - TienNAFramgia Vietnam
 
Real time server
Real time serverReal time server
Real time serverthepian
 
Python for class 11 (CBSE Computer science sub code 083)
Python for class 11 (CBSE Computer science sub code 083)Python for class 11 (CBSE Computer science sub code 083)
Python for class 11 (CBSE Computer science sub code 083)Nitin Kumar
 
Tornado Web Server Internals
Tornado Web Server InternalsTornado Web Server Internals
Tornado Web Server InternalsPraveen Gollakota
 
Tornado web
Tornado webTornado web
Tornado webkurtiss
 
Aim and objectives of teaching computer science
Aim and objectives of  teaching computer scienceAim and objectives of  teaching computer science
Aim and objectives of teaching computer scienceviji_tgce
 
Computer science project work
Computer science project workComputer science project work
Computer science project workrahulchamp2345
 
Language Teaching Approaches and Methods
Language Teaching Approaches and MethodsLanguage Teaching Approaches and Methods
Language Teaching Approaches and Methodsemma.a
 
Different approaches and methods
Different approaches and methodsDifferent approaches and methods
Different approaches and methodsswitlu
 

Andere mochten auch (12)

用Tornado开发RESTful API运用
用Tornado开发RESTful API运用用Tornado开发RESTful API运用
用Tornado开发RESTful API运用
 
Introdução a linguagem Go
Introdução a linguagem GoIntrodução a linguagem Go
Introdução a linguagem Go
 
Introduction to Tornado - TienNA
Introduction to Tornado - TienNAIntroduction to Tornado - TienNA
Introduction to Tornado - TienNA
 
Real time server
Real time serverReal time server
Real time server
 
Python for class 11 (CBSE Computer science sub code 083)
Python for class 11 (CBSE Computer science sub code 083)Python for class 11 (CBSE Computer science sub code 083)
Python for class 11 (CBSE Computer science sub code 083)
 
Tornado Web Server Internals
Tornado Web Server InternalsTornado Web Server Internals
Tornado Web Server Internals
 
Tornado web
Tornado webTornado web
Tornado web
 
Aim and objectives of teaching computer science
Aim and objectives of  teaching computer scienceAim and objectives of  teaching computer science
Aim and objectives of teaching computer science
 
Computer science project work
Computer science project workComputer science project work
Computer science project work
 
Teaching methodology
Teaching methodologyTeaching methodology
Teaching methodology
 
Language Teaching Approaches and Methods
Language Teaching Approaches and MethodsLanguage Teaching Approaches and Methods
Language Teaching Approaches and Methods
 
Different approaches and methods
Different approaches and methodsDifferent approaches and methods
Different approaches and methods
 

Kürzlich hochgeladen

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Kürzlich hochgeladen (20)

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Asynchronous programming in Python

  • 1. ASYNCHRONOUS PROGRAMMING IN PYTHON With Rocks In Wednesday, 31 August, 11
  • 2. Not really covering Tornado Wednesday, 31 August, 11
  • 6. Just mostly Twisted ... with some Tornado. Wednesday, 31 August, 11
  • 7. Introductions • Hi, I'm Aurynn • This is a hedgehog Wednesday, 31 August, 11
  • 8. I’m more like a Magpie • Shiny things are SO COOL • I could talk about how shiny they are until http://www.flickr.com/photos/cmg2011/5147250751/ Wednesday, 31 August, 11
  • 9. Look! A particularly shiny thing! Wednesday, 31 August, 11
  • 10. Buzzword Bingo • Much ado about Node.js • Event Driven! • Tornado, from Facebook! • Event loops! • “Web Scale!” Wednesday, 31 August, 11
  • 11. Lots of Chatter • Without lots of research, it’s just kind of noise http://en.wikipedia.org/wiki/File:Carl_Friedrich_Gauss.jpg Wednesday, 31 August, 11
  • 12. Past the buzzwords, useful ideas Wednesday, 31 August, 11
  • 14. Doing more than one thing at once Wednesday, 31 August, 11
  • 15. ... sort of. Wednesday, 31 August, 11
  • 16. And it’s the sort of that is important Wednesday, 31 August, 11
  • 17. A TERRIBLY BRIEF, PROBABLY INACCURATE HISTORY Wednesday, 31 August, 11
  • 18. Threading, you’ve heard of it • Really common • Java, .NET, even some Python • Super awesome! Shared memory, shared scopes, fun all around.. Wednesday, 31 August, 11
  • 19. Surprisingly good, until it isn’t • Very difficult to access shared state safely • Race conditions • Even experts have a hard time of it • Generally hard to do right Wednesday, 31 August, 11
  • 20. The kernel cares about when things happen, not you. Multiprocessing No problems with locks or race conditions, since you don’t have a consistent memory region Fork() makes life so easy! the MP model even makes multiple-systems a viable approach: it’s pretty trivial to SSH into another computer and run a program, or a batch of programs. • Let the kernel care! • Fairly easy to write MP code on unix-likes • Can even go multi-system http://www.flickr.com/photos/epw/2876377014/ Wednesday, 31 August, 11
  • 21. Hard to share Data • It’s not easy to send data between processes • Parsing stdout, or trying to get a shmem implementation working. • import multiprocessing can be.. quirky. Wednesday, 31 August, 11
  • 22. Asynchronous! • Like threading, everything is in a single process • All my Variables, All the Time • No race conditions (mostly) • Guarantee of no concurrent execution http://www.flickr.com/photos/rachelpasch/3754315974/ Wednesday, 31 August, 11
  • 23. Not all Unicorns and Rainbows Just like MP and threads, event loops have their own caveats and major constraints. Your code can’t run indefinitely, and the longer it runs, the longer your process stalls. Like threading, it’s still Not Easy to get your head around how to write asynchronous code, and this is something else we’ll go into in a bit more detail. Single mistakes, not catching your errors in The Approved Way? You can very easily trash your entire program and cause yourself to hang. Why does this happen? It comes back to the first point of You Need to Let Go. • You have to Let Go • Bending your mind to the Asynchronous Way is still hard • A single mistake can hang • Probably going to be slower. http://www.flickr.com/photos/digitalpapercuts/5737975961 Wednesday, 31 August, 11
  • 24. SO REALLY, WHAT IS ASYNC? So, I’ve made some broad generalizations about event loops, and the caveats they bring to the table. Let’s go into some more detail about what they do and are, and how those caveats actually work, and look at some code to really show how to work in the Asynchronous Way. Wednesday, 31 August, 11
  • 25. so, to get this far, we haven’t really answered the first question: what *is* an event loop? What is an event loop? Wednesday, 31 August, 11
  • 26. What is an event loop? At its heart, an event loop is just a long-running while loop, iterating over a set of callbacks, or events to be run in the future. When an event gets triggered, often in the form of a socket message, or the completion of another function, or a timeout firing. • A long-running while loop • When an event triggers, the loop catches this fact • Events are pretty generic Wednesday, 31 August, 11
  • 28. Then what? When you added the event you care about, you also added a callback. A callback is simply a Python function that gets run with the results of the event. The return value of a function, or the data coming off your socket, or whatever is what this function gets passed. The great part is that this function definition is allowed to be *any callable* object in Python. A class with .__call__, a function, a bound method on an object, whatever scope you like, it has. • Let my code know! • This code can be any callable Wednesday, 31 August, 11
  • 29. But once it’s in your code.. Wednesday, 31 August, 11
  • 30. You have to Let Go As you’ve probably figured out, what happens in an event system is analogous to co-operative multitasking. When an event fires and your callback gets run, what happens? Since it’s a standard method call, Event Loop Your Code Wednesday, 31 August, 11
  • 31. You have to Let Go control is handed over to your method, and doesn’t return to the event loop UNTIL YOU RETURN. Event Loop Your Code Wednesday, 31 August, 11
  • 32. We could be here a while... control is handed over to your method, and doesn’t return to the event loop UNTIL YOU RETURN. So let’s say your particular callback takes, oh second to do its thing, as it’s a particularly computationally intensive, your entire event loop is unable to do anything else. Let’s compute Pi to a BILLION decimal places! Your Code Wednesday, 31 August, 11
  • 33. Not just silly maths, either This happens no matter what your code does, it silly math or a web site reaching out to MySQL for data, or going to disk to open a file iterating over a long array, or even waiting on the user to do something. As long as your code hasn’t returned, your ent program has STALLED. I need some data from MySQL. Your Code Wednesday, 31 August, 11
  • 34. You have STALLED. http://www.flickr.com/photos/neilwill/5023734329/ Wednesday, 31 August, 11
  • 35. It should be fairly obvious that this is bad, and why it’s bad. To use the example of a hypothetical website, if you’re stalled waiting for the database, you can’t accept new connections, and you can’t even give an indication why. Your site will *appear* to perform slowly. Is that really bad? Wednesday, 31 August, 11
  • 36. Solving this isn’t easy, and requires adjusting your mental model on how programs flow. In Twisted, programs have to be written with the idea that a method call won’t return the results you expect, but instead an object that will tell a function what your data is. Asynchronous code is harder Wednesday, 31 August, 11
  • 37. Asynchronous code is harder For instance, x = y() won’t do what you expect. How can it, when you’re not actually • x = y() doesn’t work anymore. Wednesday, 31 August, 11
  • 38. Asynchronous code is harder In this model, you end up with very tiny functions that perform very small, discrete amounts of work, before releasing control back to the event loop. In order for these very tiny functions to be useful, we have to keep tight control over our scope, and an easy way to do that is by using closures. • x = y() doesn’t work anymore. • Requires very tiny functions Wednesday, 31 August, 11
  • 39. This is what Twisted does As you can see here, we’ve expanded our row processor into its own function, as well as adding an error handler to the t wisted Deferred. Wednesday, 31 August, 11
  • 40. The first way in which asynchronous code can be written is through the use of closures. A closure is a funky sort of internal, anonymous function that “closes over” the scope of the function it’s defined in. This can be very powerful, as the closure effectively “resumes” back in the middle of the original function, can update state, and generally do useful things. Wait, wait, what just happened? Wednesday, 31 August, 11
  • 41. What that was was a *deferred*, basically the core response that you’ll get out of an API in Twisted. What a deferred is, is an indicator that something is going to happen *later on*, as opposed to right now. This comes back to the core ideal of having to let go, and let’s go back to the code to explain further Deferred, the Core of Twisted Wednesday, 31 August, 11
  • 42. The Core of Twisted • Most APIs built on Twisted return Deferreds • Almost always involve user code Wednesday, 31 August, 11
  • 43. What that was was a *deferred*, basically the core response that you’ll get out of an API in Twisted. What a deferred is, is an indicator that something is going to happen *later on*, as opposed to right now. This comes back to the core ideal of having to let go, and let’s go back to the code to explain further But what is a Deferred? Wednesday, 31 August, 11
  • 44. But what is it? These are the basic features that you probably care about in a deferred; Add callbacks and errbacks, which we’ve already covered a bit of, and these new methods, .callback and .errback. Wednesday, 31 August, 11
  • 45. For instance, x = y() won’t do what you expect. Segue Power! How can it, when you’re not actually • .callback starts the callback chain • .errback causes the callback chain to explode and die messily Wednesday, 31 August, 11
  • 46. What that was was a *deferred*, basically the core response that you’ll get out of an API in Twisted. What a deferred is, is an indicator that something is going to happen *later on*, as opposed to right now. This comes back to the core ideal of having to let go, and let’s go back to the code to explain further errback is structurally identical to callbacks Wednesday, 31 August, 11
  • 47. Let’s look at this again Wednesday, 31 August, 11
  • 48. What that was was a *deferred*, basically the core response that you’ll get out of an API in Twisted. What a deferred is, is an indicator that something is going to happen *later on*, as opposed to right now. This comes back to the core ideal of having to let go, and let’s go back to the code to explain further What’s the key here? Wednesday, 31 August, 11
  • 49. What that was was a *deferred*, basically the core response that you’ll get out of an API in Twisted. What a deferred is, is an indicator that something is going to happen *later on*, as opposed to right now. This comes back to the core ideal of having to let go, and let’s go back to the code to explain further It doesn’t happen right away. Wednesday, 31 August, 11
  • 51. And again, for comparison Wednesday, 31 August, 11
  • 52. What that was was a *deferred*, basically the core response that you’ll get out of an API in Twisted. What a deferred is, is an indicator that something is going to happen *later on*, as opposed to right now. This comes back to the core ideal of having to let go, and let’s go back to the code to explain further Composition Wednesday, 31 August, 11
  • 53. What that was was a *deferred*, basically the core response that you’ll get out of an API in Twisted. What a deferred is, is an indicator that something is going to happen *later on*, as opposed to right now. This comes back to the core ideal of having to let go, and let’s go back to the code to explain further Or, chaining callbacks Wednesday, 31 August, 11
  • 55. What that was was a *deferred*, basically the core response that you’ll get out of an API in Twisted. What a deferred is, is an indicator that something is going to happen *later on*, as opposed to right now. This comes back to the core ideal of having to let go, and let’s go back to the code to explain further Synchronous callback chain Wednesday, 31 August, 11
  • 56. ASYNCHRONOUS != FASTER The final caveat on our List of Asynchronous Problems is the idea I’ve run into that asynchronous code is, by the very fact of running on an evented IO ser ver, it will be faster. This idea is all sorts of wrong. Synchronous code that simply runs inside of a event- driven IO system like Twisted or Tornado is naturally going to be slower than the same code running standalone. Wednesday, 31 August, 11
  • 57. Synchronous code that simply runs inside of a event-driven IO system like Twisted or Tornado is naturally going to be slower than the same code running standalone. The event loop is overhead Wednesday, 31 August, 11
  • 58. And, as you’ve already seen on how to structure asynchronous programs, effectively useless, unless you take the time to program to take advantage of an asynchronous event loop. The event loop is overhead... ..and without proper coding, useless Wednesday, 31 August, 11
  • 59. The question you all want to ask Wednesday, 31 August, 11
  • 60. WHY BOTHER? Why spend extra time doing it the Hard Way, the way where you are required to do more work and write code in completely new ways? Wednesday, 31 August, 11
  • 61. The real advantage, the real power of asynchronous programming is the level of scale to which you can go. No threads means no thread overhead, and no complexity of maintaining locks and trying to share state. Nginx, well-regarded as one of the fastest webser vers around, is entirely built around asynchronous programming. Scales beautifully Wednesday, 31 August, 11
  • 62. Once you really “get it”, the entire idea starts seeming terribly elegant and worthwhile, and you start looking for how to process code asynchronously in all aspects of your programming. Terribly elegant Wednesday, 31 August, 11
  • 63. You also end up in a position where you’re writing far more reusable code. Why? Well, you need to have these functions which run as callbacks, and as we’ll go into in a little bit, those same callbacks can be chained together. There’s very little point in rewriting code all the time to More re-usable code Wednesday, 31 August, 11
  • 64. What do I mean by this? Your code is often going to be waiting for other servers - webser vers, database ser vers, net work, file, Everything. So the example I have the Closer mapping to reality Wednesday, 31 August, 11
  • 65. LITTLE BITS OF TORNADOS Since I’ve spent most of my time so far talking about Twisted as opposed to the other “major” asynchronous platform, I’d like to devote a little bit of time to Tornado. Wednesday, 31 August, 11
  • 66. Tornado, while it does have an internal IO loop, and libraries *do* use it standalone, the vast majority of examples you’ll run across take the idea of it being a web framework akin to Pylons or Pyramid or Bottle. Event loop + web framework Wednesday, 31 August, 11
  • 68. Not as wide library support Wednesday, 31 August, 11
  • 69. Lacks a deferred metaphor Wednesday, 31 August, 11
  • 70. Tornado, while it does have an internal IO loop, and libraries *do* use it standalone, the vast majority of examples you’ll run across take the idea of it being a web framework akin to Pylons or Pyramid or Bottle. .add_callback(my_function) Wednesday, 31 August, 11
  • 71. my_function has to handle both Wednesday, 31 August, 11
  • 72. my_function( response, error ) Wednesday, 31 August, 11
  • 73. So why Tornado over Twisted? Wednesday, 31 August, 11
  • 74. Speed. http://programmingzen.com/2009/09/13/benchmarking-tornado-vs-twisted-web-vs-tornado-on-twisted/ Wednesday, 31 August, 11