[Twisted-Python] Limiting a task.Cooperator to N work units / sec

Phil Mayers p.mayers at imperial.ac.uk
Fri Oct 1 05:41:11 EDT 2010

Is there an easy way to make a task.Cooperator instance only execute N 
ticks / sec, summed across all iterators it's driving? So if you add two 
iterators, they each run at N/2 per sec, 3 at N/3, etc.

It seems like this ought to do it:

N = <rate>

def myScheduler(x):
   # reschedule N times per second
   reactor.callLater(1.0/N, x)

class myTermination:
   def __call__(self):
     # stop immediately after one iterator.next()
     return True

myCoop = task.Cooperator(

...but this doesn't seem to work when I try it:

def mytask(name, limit):
     for i in range(limit):
         print name, time.time(), i
         yield i

def running():
     finish_a = myCoop.coiterate(mytask('a', 40))
     def start_b():
         finish_b = myCoop.coiterate(mytask('b', 10))
     reactor.callLater(6.5, start_b)

def main():

if __name__=='__main__':

...shows that, once the "b" iterator is added, each iterator is running 
1/sec, rather than the whole cooperator:

a 1285925864.05 0
a 1285925865.05 1
a 1285925866.05 2
a 1285925867.05 3
a 1285925868.05 4
a 1285925869.05 5 << running fine 1/sec up until here
b 1285925870.05 0
a 1285925870.55 6 << now running 2/sec
b 1285925871.05 1
a 1285925871.55 7
b 1285925872.05 2
a 1285925872.55 8
b 1285925873.05 3

I've had a look at the source code, and it looks like the logic should 
cause what I want to happen, but obviously it's not. Version is Twisted 
8.2.0 on python 2.6 (Fedora 12)

More information about the Twisted-Python mailing list