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

Phil Mayers p.mayers at imperial.ac.uk
Fri Oct 1 08:58:15 EDT 2010

On 01/10/10 13:46, exarkun at twistedmatrix.com wrote:
> On 09:41 am, p.mayers at imperial.ac.uk wrote:
>> 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:
> Very close!  It took me a while to notice the mistake.
>> N =<rate>
>> def myScheduler(x):
>>    # reschedule N times per second
>>    reactor.callLater(1.0/N, x)
> The scheduler function must not return None!  This confuses the
> scheduling code in cooperator and causes it to run too many things in
> parallel.  This mistake could be detected easily by cooperator and a

Ah ha! In fact, it looks like the scheduler must return an object with a 
.cancel() method (obviously an IDelayedCall in the usual case).

I'll open a ticket; presumably it would a) be nice to detect invalid 
return values and b) update the docstring of Cooperator.__init__?

More information about the Twisted-Python mailing list