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

Twisted Logic: Endpoints and Why You Shouldn't Be Scared of Twisted

Twisted Logic: Endpoints and Why You Shouldn't Be Scared of Twisted

This talk will be a survey of my learning experience adding new endpoint APIs to Twisted, an event-driven networking engine (as a Google Summer of Code project), with a special focus on the analysis of some of the horror stories that surround Twisted. Right from the asynchronous I/O model to Deferreds: if it scares you, we’ll figure a way out and see what the makers of Twisted say when confronted.

Ashwini Oruganti

March 15, 2013
Tweet

More Decks by Ashwini Oruganti

Other Decks in Programming

Transcript

  1. Twisted Logic - Ashwini Oruganti @_ashfall_ PyCon 2013

  2. Twisted asynchronous event-driven networking framework

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

  4. Google Summer of Code spend the summer working on an

    open source project with a mentoring organization
  5. Endpoint an interface with a single method that takes an

    argument returns*: a listening port / a connected protocol
  6. endpoint = TCP4ServerEndpoint(reactor, 8007) endpoint.listen(Factory()) endpoint = TCP4ClientEndpoint(reactor, "localhost", 8007)

    endpoint.connect(Factory()) Server Client
  7. class TCP4ServerEndpoint(object): """ Implements TCP server endpoint with an IPv4

    configuration """ ... def __init__(self, reactor, port, backlog=50, interface=''): ... def listen(self, protocolFactory): return defer.execute( self._reactor.listenTCP, ...)
  8. class TCP6ServerEndpoint(object): """ Implements TCP server endpoint with an IPv6

    configuration """ ... def __init__(self, reactor, port, backlog=50, interface='::'): ... def listen(self, protocolFactory): return defer.execute( self._reactor.listenTCP, ...)
  9. None
  10. It’s just code.

  11. class StandardIOEndpoint(object): """ A Standard Input/Output endpoint """ implements(interfaces.IStreamServerEndpoint) def

    __init__(self, reactor): ... def listen(self, stdioProtocolFactory): return defer.execute(stdio.StandardIO, stdioProtocolFactory.buildProtocol( PipeAddress()))
  12. Moral of the story: Do not get flustered. Do not

    overthink.. Forget it's Twisted
  13. Read the code. Solve the problem (write code) Moral of

    the story:
  14. There’s nothing more boring than the comfort zone

  15. TCP Client Endpoint IPv6 addresses Hostname Resolution

  16. class TCP6ClientEndpoint(object): def __init__(self, reactor, host, port, timeout=30, bindAddress=None): ...

    def connect(self, protocolFactory): """ Connect via TCP, once the hostname resolution is done. """ ...
  17. def _nameResolution(self, host): """ Resolve the hostname string into a

    tuple containing the host IPv6 address. """ ... def _resolvedHostConnect(self, resolvedHost, protocolFactory): """ Connect to the server using the resolved hostname. """ ...
  18. Deferred Callbacks and Errbacks Flow is not obvious Debugging is

    tricky
  19. class SomeState(object): def __init__(self): self.values = [] def getValue(self): return

    self.values.pop(0) def addValue(self, value): self.values.append(value)
  20. def foo(): return SomeState() def bar(): state = foo() print

    state.getValue()
  21. Firing the Deferred def foo(): s = SomeState() s.addValue(2) return

    s
  22. It’s just code.

  23. It’s 'X', when it isn't really 'X'

  24. It’s full of highly complex concepts!

  25. “It was created by a couple of dudes who dropped

    out of school, and a 16-year-old. HOW HARD COULD IT BE?”
  26. None
  27. It’s foreign!

  28. camelCase PEP8: July 5, 2001 Twisted’s coding standard: May 2,

    2001
  29. It’s huge!

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

  31. Know what you're doing.

  32. None
  33. <conclusion />