[PEAK] Events Question: How bad of an idea is this?

Chad Rosenberg chad at idrankwhat.net
Wed Nov 10 20:50:25 EST 2004

On Wed, 10 Nov 2004, Phillip J. Eby wrote:

> >With this change the above sample code runs to completion without any
> >errors, as does the code I am working with.  I'm not sure of what, if any,
> >long term repercussion this may produce (stack overflows, infinite loops,
> >etc).  Hence my question, how bad of an idea is this?
> Hm.  Well, that depends on what you're doing with the Twisted side of
> things.  Keep in mind that when you use a nested 'runUntil()', the reactor
> is going to fire shutdown events as soon as the *inner* runUntil()
> finishes.  This may not be what you want, since I believe that it will
> close all current socket connections.  Personally, I don't think that this
> would be a useful feature to add to PEAK.  :)

Hmm, that didn't come up.  I just added a simple echo server to my sample
code and Twisted 1.2.0 maintained all open socket connections throughout
all the eventLoop calls.

  - Chad

from peak.api          import binding, commands, events

from twisted.internet.protocol import Protocol, Factory

class Echo(Protocol):
    def dataReceived(self, data):
        print 'Echo:', data

class Test(commands.EventDriven):
    eventLoop  = binding.Obtain(events.IEventLoop)
    reactor    = binding.Obtain(commands.ITwistedReactor,

    def daemon(self):
        while True:
            yield self.eventLoop.sleep(1); events.resume()
            print 'daemon'

            print self.something

    daemon = binding.Make(events.taskFactory(daemon))

    def something(self):
        self.eventLoop.runUntil(self.eventLoop.sleep(5), True)
        return 'something'
    something = binding.Make(something)

    def _run(self):
        factory = Factory()
        factory.protocol = Echo
        self.reactor.listenTCP(54321, factory)

        self.eventLoop.runUntil(self.eventLoop.sleep(15), True)
        self.eventLoop.runUntil(self.eventLoop.sleep(3), True)
        print 'boo'
        self.eventLoop.runUntil(self.eventLoop.sleep(30), True)
        return 0

More information about the PEAK mailing list