[Twisted-Python] Re-Occuring Operations

Andrew Bennetts andrew-twisted at puzzling.org
Mon Mar 17 07:46:45 EST 2003


On Mon, Mar 17, 2003 at 06:42:17AM +0000, Clark C. Evans wrote:
> Hello.   I was wondering if there is anything which handles 
> re-occuring functions; that is, an auto-rescheduler or
> something of that nature.   Here is what I came up with,
> I'm sure there is a shorter 'lambda' form, but I was
> going for clarity here.

There is.  This is copied straight from doc/howto/time.html:

"""
    If we want a task to run every X seconds repeatedly, we can
    just re-add it every time it's run:

        from twisted.internet import reactor
        
        def runEverySecond():
            print "a second has passed"
            reactor.callLater(1, runEverySecond)
        
        reactor.callLater(1, runEverySecond)
"""


>   class MultiRun:
>       """ apply a callable while it returns true """
>       def __init__(self, callable, waitInterval = 0):
>           self.callable = callable
>           self.waitInterval = waitInterval
>           self.iterate(starting = 1)
>       def iterate(self, starting = 0):
>           if starting or self.callable():
>               from twisted.internet import reactor
>               reactor.callLater(self.waitInterval, self.iterate)

Or:

    from __future__ import nested_scopes  # For you retro Python 2.1 hippies
    
    def multiRun(interval, func):
        from twisted.internet import reactor
        def iterate():
            if func():
                reactor.callLater(interval, iterate)
        iterate()

Except to fit in with the Twisted naming scheme, you'd have to call it
'mindlessReruns' ;)

On the other hand, being a fairly straightforward six-line function, it
probably doesn't need to be in Twisted.

> Something like this would be useful, it's already emerged in
> two different contexts in my code:
> 
>    (a) a background operation (which never returns true)
>        that runs every 5 minutes, 

That's amply catered for by the first example, possibly with a try/finally
thrown in.

>    (b) an operation which keeps pooling a resource, if 
>        the resource is busy it returns true, otherwise 
>        it processes the resource and returns false to
>        signal that it is done

I don't quite understand your terminology.  I think you mean there's a queue
of operations that can only be processed if there's some sort of resource
available to process them, and you wish to occasionally poll the resource
pool to see if there's one free.  If so, it's probably better to arrange
your code to avoid polling, i.e. make the resource pool check the queue
automatically when a resource is freed.  This reduces latency and is usually
simpler.

Regardless, I'm not sure this deserves or requires special support in
Twisted.  Merely documenting the necessary idioms is probably better.  After
all there are lots of possible small variations (e.g. dynamically varying
the interval based on the function's return value) that would be tedious to
build into a generic class or function, but are dead simple to build into
your own six-line implementation. 

Or perhaps I'm wrong, and there's a nice class with lots of use-cases just
waiting to be implemented :)

-Andrew.





More information about the Twisted-Python mailing list