$30 off During Our Annual Pro Sale. View Details »

Asynchronous Event-driven Architecture - The Twisted Way

Asynchronous Event-driven Architecture - The Twisted Way

This talk is aimed at explaining the event-driven architecture and asynchronous model, the reactor-pattern, and the basic concepts that make understanding and writing Twisted programs easier, and less abstruse and esoteric.

Picking up clues from the numerous attempts in the past to demystify the popular beliefs about the occult nature of Twisted, the talk is designed to help you acquire the “mental model” required to write and comprehend asynchronous code. We will start by developing a basic understanding of the synchronous, threaded, and asynchronous models, then move on to comparing and contrasting the former two with the latter.

Further, we will try and understand the philosophy and motivation behind the asynchronous event-driven model, and discuss the Twisted reactor and event-loop architecture. Finally, we'll try to contemplate and demystify "The Twisted Way" of doing things bolstered by excerpts from Tao Te Ching and The Tao of Programming.

Ashwini Oruganti

October 13, 2013
Tweet

More Decks by Ashwini Oruganti

Other Decks in Programming

Transcript

  1. The Twisted Way
    - Ashwini Oruganti
    @_ashfall_
    PyCon Ireland 2013

    View Slide

  2. Twisted
    asynchronous event-driven
    networking framework

    View Slide

  3. Twisted
    asynchronous event-driven
    networking framework
    aka... HARD

    View Slide

  4. The Need
    Twisted Reality: The Game
    Threads and Java
    The sadness

    View Slide

  5. The Beginning
    select()
    asyncore and Medusa

    View Slide

  6. The Beginning
    Medusa:
    Unmaintained and direct socket
    manipulation

    View Slide

  7. The Beginning

    Twisted

    View Slide

  8. Programming Models
    Single-threaded
    Multi-threaded
    Asynchronous

    View Slide

  9. Single-threaded
    time

    View Slide

  10. Single-threaded

    View Slide

  11. Multi-threaded
    time

    View Slide

  12. Multi-threaded

    View Slide

  13. Event-driven
    time

    View Slide

  14. Event-driven

    View Slide

  15. The Reactor
    Waits on and handles events
    abstracts away platform-
    specific behavior

    View Slide

  16. Event-loop
    while True:
    timeout = time_until_next_timed_event()
    events = wait_for_events(timeout)
    events += timed_events_until(now())
    for event in events:
    event.process()

    View Slide

  17. “Twisted softens your
    brains and makes you feel
    like your head is actually a
    blender optimized for gray
    gloop.”

    View Slide

  18. “It was created by a couple of
    dudes who dropped out of
    school, and a 16-year-old.
    HOW HARD COULD IT BE?”

    View Slide

  19. The Box Metaphor

    View Slide

  20. “How do I even get this
    thing to do anything?”

    View Slide

  21. When you look for it, there is nothing to see.
    When you listen for it, there is nothing to
    hear.
    When you use it, it is inexhaustible.

    View Slide

  22. from twisted.internet import reactor
    reactor.run()
    The World’s Largest NOP

    View Slide

  23. “The Master does his job and then stops.
    He understands that the universe is
    forever out of control.”

    View Slide

  24. It’s humongous!

    View Slide

  25. “Core can fit into a floppy
    disk.”

    View Slide

  26. It’s just code.

    View Slide

  27. Know what you're
    doing.

    View Slide

  28. View Slide


  29. View Slide