Deferred Gratification
Patterns of asynchronous flow of control
with Twisted deferreds

Terry Jones
terry@fluidinfo.com
@terr...
Not an intro talk. Sorry!
• Assume you understand Deferreds
• Assume you understand async
Read defer.py
• Lots of people don’t
• Much of it is beautiful
• Deferreds seem cryptic
• They’re are actually very simple
Building blocks
    Simple functions that help with deferreds
•    succeed
•    fail
•    maybeDeferred

    Classes that ...
Deferreds let you replace
synchronous data structures
 with elegant asynchrnous
        equivalents
Some of our classes
http://jon.es/other/europython-2010-deferreds.tar.bz2


• tee
• RetryingCall
• DeferredPooler


      ...
DeferredPooler
 client



 client      server   whatever



 client
DeferredPooler
 client   fe
            tc
               h(
                 x,
                    y)



 client        ...
DeferredPooler
 client   fe
            tc
               h(
                 x,
                    y)


                ...
DeferredPooler
 client   fe
            tc
               h(
                 x,
                    y)


          fetch(...
DeferredPooler
 client   fe
            tc
                 h(
                   x,
                          y)


      ...
DeferredPooler
 client   fe
            tc
                 h(
                   x,
                          y)


      ...
DeferredPooler
 client    fe
              tc
          re     h(
            su      x,
               lt      y)


     ...
Normal Deferred
1     d = someFunc(arg1,arg2)

          cb1   cb2   cb3



2         d.addCallback()
    cb1   cb2   cb3 ...
Controllable Deferred
1   d = ControllableDeferred(someFunc,arg1,arg2)

             cb1     cb2    cb3



2             d...
RetryingCall
Replace something like this
d = getPage(url)

With this
r = RetryingCall(getPage, url)
d = r.start()
Resizable Dispatch Queue
                 Tickery adder                  RDQ

                                            ...
Upcoming SlideShare
Loading in...5
×

Deferred Gratification

2,627

Published on

Slides from a talk titled "Deferred Gratification" by Terry Jones at EuroPython 2010 in Birmingham

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,627
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
22
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
































  • Deferred Gratification

    1. 1. Deferred Gratification Patterns of asynchronous flow of control with Twisted deferreds Terry Jones terry@fluidinfo.com @terrycojones
    2. 2. Not an intro talk. Sorry! • Assume you understand Deferreds • Assume you understand async
    3. 3. Read defer.py • Lots of people don’t • Much of it is beautiful • Deferreds seem cryptic • They’re are actually very simple
    4. 4. Building blocks Simple functions that help with deferreds • succeed • fail • maybeDeferred Classes that build on simple deferreds • DeferredList • DeferredQueue • DeferredSemaphore • DeferredLock
    5. 5. Deferreds let you replace synchronous data structures with elegant asynchrnous equivalents
    6. 6. Some of our classes http://jon.es/other/europython-2010-deferreds.tar.bz2 • tee • RetryingCall • DeferredPooler } • ControllableDeferred • DeferredPool rdq.py • ResizableDispatchQueue
    7. 7. DeferredPooler client client server whatever client
    8. 8. DeferredPooler client fe tc h( x, y) client server whatever client
    9. 9. DeferredPooler client fe tc h( x, y) fetch(x,y) client server whatever client
    10. 10. DeferredPooler client fe tc h( x, y) fetch(x,y) fetch(x,y) client server whatever client
    11. 11. DeferredPooler client fe tc h( x, y) fetch(x,y) fetch(x,y) client server whatever , y) ( x ch f et client
    12. 12. DeferredPooler client fe tc h( x, y) fetch(x,y) fetch(x,y) client server whatever result , y) ( x ch f et client
    13. 13. DeferredPooler client fe tc re h( su x, lt y) fetch(x,y) fetch(x,y) client server whatever result result , y) x h( tc fe ult r es client
    14. 14. Normal Deferred 1 d = someFunc(arg1,arg2) cb1 cb2 cb3 2 d.addCallback() cb1 cb2 cb3 cb4 cb5 3 # Hope d fires!
    15. 15. Controllable Deferred 1 d = ControllableDeferred(someFunc,arg1,arg2) cb1 cb2 cb3 2 d.addCallback() cb1 cb2 cb3 ? cb4 cb5 3 # If call to someFunc fires, call d. # Allow caller to call d.cancel
    16. 16. RetryingCall Replace something like this d = getPage(url) With this r = RetryingCall(getPage, url) d = r.start()
    17. 17. Resizable Dispatch Queue Tickery adder RDQ pending WWW put put a b c d underway Twitter e doWork f FluidDB call g admin pause, resume, stop, size, widen, narrow, underway, cancel
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×