Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Asynchronous Plumber - PyConUK 2012

Server Density
September 28, 2012

The Asynchronous Plumber - PyConUK 2012

As internal HTTP REST services, popularised by Amazon, have been gaining a lot of attention - newly emerging HTTP/1.1 extensions like Server Sent Messages and WebSockets are the perfect accompaniment to asynchronous tech stacks like Tornado, Twisted, Gevent and Node.js, not just for powering 10 minute chat demos.

Presented at http://2012.pyconuk.net/ by Wes Mason.

Server Density

September 28, 2012
Tweet

More Decks by Server Density

Other Decks in Technology

Transcript

  1. THE WEB IS OUR API Everything does HTTP REST is

    great - let's do more of this Almost everything can do HTTP asynchronously
  2. BRAVE NEW (ASYNC) WORLD Longpolling, WebSockets and Server Sent Events

    UIs are responsive or GTFO Libraries like socket.io make this pretty easy for devs
  3. COME BACK TO ME Jobs running in parallel using task

    systems What we want: async responses to the browser What happens: Jobs complete and...die. *sniff*
  4. COME BACK TO ME (CONT.) HTTP or WebSocket request kicks

    off a task Task runs asynchronous to requests Task completed and returns data... ...how?
  5. SEGWAY TO CELERY Great: Takes care of distributing tasks to

    clusters Not so great: can't do multiple pubsub
  6. PUBLISH AND SUBSCRIBE Need results back from Celery by account

    ID Pubsub is ideal for this Each account has multiple users What we want: one task per account, same result to each user
  7. MANY TO MANY WITH PUBSUB Redis let's you do this

    (but we don't know Redis) In theory AMQP let's you do M2M pubsub (and Celery does AMQP by default)
  8. ...WELL, KIND OF > > > r e s u

    l t = t a s k . d e l a y ( f o o ) A s y n c R e s u l t ( i d = " s o m e - u n i q u e - i d " ) > > > r e s u l t . g e t ( ) . . .
  9. CUE THE HAXX Threads (urgh) Polling MongoDB (we do know

    Mongo) Using MongoDB as Celery backend and keeping results (hello race conditions!)
  10. YOU HAD ME HOOKED The stack's just web APIs.. ..why

    not just use webhooks to return data? They're async, just like handling our frontend requests They can be tested the same way as our other APIs
  11. THE WEBHOOK REGISTRY Register/unregister callbacks by a given key (e.g.

    an account ID) Web handler accepts data payloads via HTTP POST Webhook URI persisted somewhere (e.g. MongoDB)
  12. SKYHOOOKS TO REACH THE TORNADO Open source library with registry

    for Tornado - soon Gevent and Twisted. Registers callbacks and handling of multiple instance URIs Available now: github.com/serverdensity/skyhooks
  13. MEANWHILE, BACK ON THE [SERVER] FARM Tasks running somewhere, somehow

    Data returning from one or more tasks One or more I N S E R T _ S E R V E R _ H E R E (e.g. Tornado) instances at one or more URIs awaiting data
  14. # i m p o r t r e q

    u e s t s , i m p o r t p y m o n g o # s e t u p c o l l e c t i o n e t c . e t c . h o o k s = w e b h o o k _ c o l l e c t i o n . f i n d ( { " a c c o u n t _ i d " : i d } ) i f d a t a a n d l e n ( d a t a ) > 0 : f o r h o o k i n h o o k s : r e q u e s t s . p o s t ( h o o k [ " u r l " ] , d a t a ) N.B. s k y h o o k s does this for you