[Twisted-Python] twisted and functional programming idioms

Rodney D Price rodprice at raytheon.com
Wed Jul 9 21:26:44 EDT 2008

I'm just getting started with Twisted, and trying to find
my way around.  I've done quite a bit of functional 
programming (Haskell), where recursion is a very
common idiom.  I keep finding myself heading in that
direction as I try to understand how to use Twisted.
For example, if I wanted to write a loop in Haskell
that woke up once a second and did something, I
might write

main = heartbeat
heartbeat = do
  sleep 1000

This code first sleeps for one second, then does
something, then calls itself.  (The Haskell compiler
knows how to keep the stack from overflowing here,
in case anyone is wondering.)

So if I wanted to do something similar in Twisted,
my first inclination is to use callLater,

def heartbeat(reactor):
    reactor.callLater(1, heartbeat, reactor)

But then my head starts hurting, because I keep 
thinking that I have to set up all the callbacks
before I start the reactor.  This code sets callbacks 
at runtime.  Will that be a problem?


P.S.  I know there's probably a function somewhere
in Twisted to do this already, but I'm not asking how
to do this particular thing.  I'm really asking if thinking
in terms of recursion is a useful Twisted idiom.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20080709/f97116ff/attachment.htm 

More information about the Twisted-Python mailing list