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

Phil Mayers p.mayers at imperial.ac.uk
Sun Oct 3 07:41:15 MDT 2010


On 10/01/2010 02:27 PM, exarkun at twistedmatrix.com wrote:
> On 12:58 pm, p.mayers at imperial.ac.uk wrote:
>> 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__?
>
> Sounds great.

I opened:

http://twistedmatrix.com/trac/ticket/4666

...and attached a simple unit test & patch/docstring update.




More information about the Twisted-Python mailing list