Limiting thread intruction count

Neil Macneale mac4-devnull at theory.org
Sat Jul 7 16:40:53 EDT 2001


In article <mailman.994530972.4782.python-list at python.org>, "Tim Peters"
<tim.one at home.com> wrote:

> On any platform with thread support, Python code runs under the
> protection of a global interpreter lock (GIL), which gives a thread
> exclusive access to the Python Virtual Machine for a fixed number of
> bytecodes.  You can alter that magic number by calling
> sys.setcheckinterval().  Say you set it to 100. Then every 100
> bytecodes, the eval loop forces the current thread to release the GIL,
> allowing some other thread to acquire it.  Python has no control over
> *which* thread gets it next -- since Python threads are real OS threads,
> that's up to the OS thread implementation.  So is it round robin? Beats
> me, and, more importantly, beats you too <wink>.
> 
> The Stackless Python variant
> 
>     http://www.stackless.com/
> 
> implements a notion of (pseudo)microthreads instead, which may be more
> to your liking.  They're not related to OS threads at all, and typically
> enjoy much lower memory consumption and much faster context switching
> than "real threads".  Also more potential for influencing exactly when
> they swtich.

I was actually taking a look at stackless python because I expect this
program to have lots of thread, like thousands.  I presume since you are
suggesting it that it supports the setcheckinterval?

Is there posibly a function that is called when a thread gives up the
lock which I could use to take the thread out of the running queue?  That
would be the place I would like to run some maintenance.

> 
> Note that "a bytecode" is a very high-level thing in Python, and number
> of bytecodes has no clear relationship to elapsed time.  For example,
> one bytecode may add 1+1, while another may sort a million-element list.
> 

I realize that.  I figure that the programmers who know how to better
execute each byte code instruction should be given the advantage.  This
game is supposed to reward people who know what they are doing.  We just
need a way of keeping them in check by monitoring the number of
instruction somehow.

Neil Macneale

--
To reply to me via email, remove the '-devnull' from my address.



More information about the Python-list mailing list