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

Chad Rosenberg chad at idrankwhat.net
Wed Nov 10 19:24:09 EST 2004


The code I have been working on uses quite a bit of generators wrapped by
taskFactory and I am finding it increasing necessary to be able to call a
variety of other nested methods from those generators that make calls
involving Twisted Deferreds and EventLoop.runUntil in twisted_support.py.

Take, for instance, this sample code:

----------------------
from peak.api import binding, commands, events

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

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

            print self.something

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

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

    def _run(self):
        self.daemon
        self.eventLoop.runUntil(self.eventLoop.sleep(3), True)
        print 'boo'
        self.eventLoop.runUntil(self.eventLoop.sleep(30), True)
        return 0
----------------------

When I run the above example with Peak 0.5a3 under Gentoo Linux and
Python 2.3.3 I get a "StopIteration:  Unexpected reactor exit" in
"peak/events/twisted_support.py", line 191, in runUntil.

Trying to get it to work, I made the following change to
EventLoop.runUntil in twisted_support.py:

        if not exit:
            self.reactor.run(False)

  -- to --

        while not exit:
            self.reactor.run(False)

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?


Thanks,
Chad





More information about the PEAK mailing list