twisted logic
TRANSCRIPT
Twisted Logic
- Ashwini Oruganti @_ashfall_
PyCon 2013
Twisted
asynchronous event-driven networking framework
Twisted
asynchronous event-driven networking framework
aka... HARD
Google Summer of Code
spend the summer working on an open source project with a
mentoring organization
Endpoint
an interface with a single method that takes an argument
returns*: a listening port / a connected protocol
endpoint = TCP4ServerEndpoint(reactor, 8007)endpoint.listen(Factory())
endpoint = TCP4ClientEndpoint(reactor,"localhost", 8007)
endpoint.connect(Factory())
Server
Client
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, ...)
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, ...)
It’s just code.
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()))
Moral of the story:
Do not get flustered.
Do not overthink..
Forget it's Twisted
Read the code.
Solve the problem(write code)
Moral of the story:
There’s nothing more boring
than the comfort zone
TCP Client Endpoint
IPv6 addresses
Hostname Resolution
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.
"""
...
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.
"""
...
Deferred
Callbacks and Errbacks
Flow is not obvious
Debugging is tricky
class SomeState(object):
def __init__(self):
self.values = []
def getValue(self):
return self.values.pop(0)
def addValue(self, value):
self.values.append(value)
def foo():
return SomeState()
def bar():
state = foo()
print state.getValue()
Firing the Deferred def foo():
s = SomeState()
s.addValue(2)
return s
It’s just code.
It’s 'X', when it isn't
really 'X'
It’s full of highly complex concepts!
“It was created by a couple of dudes who dropped out of school, and a 16-year-old.
HOW HARD COULD IT BE?”
It’s foreign!
camelCasePEP8: July 5, 2001
Twisted’s coding standard: May 2, 2001
It’s huge!
“Core can fit into a floppy disk.”
Know what you're doing.
<conclusion />