Recommended way to force a thread context switch?

Duncan Booth duncan.booth at invalid.invalid
Sat Jan 6 17:33:26 EST 2007


Lloyd Zusman <ljz at asfast.com> wrote:

>> Why do you think that just letting the threads run won't have the
>> effect you desire? Leave it to the system to schedule the threads.
> 
> I can already see that they don't have the effect I desire.  They are
> long numerical calculations in tight loops.  I have to periodically
> put explicit time.sleep(0.001) calls in place to force the context
> switching, and I was wondering if that's the recommended method.
> 
Not really.

If the context isn't switching enough for you then try calling 
sys.setcheckinterval(n) with varying values of n until you find one which 
is suitable. Calling it with a lower value of n will increase the frequency 
that you switch thread contexts, although of course it will also increase 
the overall runtime for your program.

Alternatively you could try splitting your processing into smaller chunks 
and ensure each thread does a small chunk at a time instead of a large one.

Why does it matter whether individual threads are being 'starved'? Surely 
you want them all to complete in any case, so does it matter if they run 
sequentially or in parallel?




More information about the Python-list mailing list