twisted logic

33
Twisted Logic - Ashwini Oruganti @_ashfall_ PyCon 2013

Upload: ashfall

Post on 08-May-2015

164 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Twisted logic

Twisted Logic

- Ashwini Oruganti @_ashfall_

PyCon 2013

Page 2: Twisted logic

Twisted

asynchronous event-driven networking framework

Page 3: Twisted logic

Twisted

asynchronous event-driven networking framework

aka... HARD

Page 4: Twisted logic

Google Summer of Code

spend the summer working on an open source project with a

mentoring organization

Page 5: Twisted logic

Endpoint

an interface with a single method that takes an argument

returns*: a listening port / a connected protocol

Page 6: Twisted logic

endpoint = TCP4ServerEndpoint(reactor, 8007)endpoint.listen(Factory())

endpoint = TCP4ClientEndpoint(reactor,"localhost", 8007)

endpoint.connect(Factory())

Server

Client

Page 7: Twisted logic

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, ...)

Page 8: Twisted logic

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, ...)

Page 9: Twisted logic
Page 10: Twisted logic

It’s just code.

Page 11: Twisted logic

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()))

Page 12: Twisted logic

Moral of the story:

Do not get flustered.

Do not overthink..

Forget it's Twisted

Page 13: Twisted logic

Read the code.

Solve the problem(write code)

Moral of the story:

Page 14: Twisted logic

There’s nothing more boring

than the comfort zone

Page 15: Twisted logic

TCP Client Endpoint

IPv6 addresses

Hostname Resolution

Page 16: Twisted logic

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.

"""

...

Page 17: Twisted logic

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.

"""

...

Page 18: Twisted logic

Deferred

Callbacks and Errbacks

Flow is not obvious

Debugging is tricky

Page 19: Twisted logic

class SomeState(object):

def __init__(self):

self.values = []

def getValue(self):

return self.values.pop(0)

def addValue(self, value):

self.values.append(value)

Page 20: Twisted logic

def foo():

return SomeState()

def bar():

state = foo()

print state.getValue()

Page 21: Twisted logic

Firing the Deferred def foo():

s = SomeState()

s.addValue(2)

return s

Page 22: Twisted logic

It’s just code.

Page 23: Twisted logic

It’s 'X', when it isn't

really 'X'

Page 24: Twisted logic

It’s full of highly complex concepts!

Page 25: Twisted logic

“It was created by a couple of dudes who dropped out of school, and a 16-year-old.

HOW HARD COULD IT BE?”

Page 26: Twisted logic
Page 27: Twisted logic

It’s foreign!

Page 28: Twisted logic

camelCasePEP8: July 5, 2001

Twisted’s coding standard: May 2, 2001

Page 29: Twisted logic

It’s huge!

Page 30: Twisted logic

“Core can fit into a floppy disk.”

Page 31: Twisted logic

Know what you're doing.

Page 32: Twisted logic
Page 33: Twisted logic

<conclusion />