locks

Cliff Wells clifford.wells at comcast.net
Wed Oct 13 14:24:27 CEST 2004


On Wed, 2004-10-13 at 14:11 +0200, Diez B. Roggisch wrote:
> Ajay wrote:
> 
> > what would happen if i try to access a variable locked by another thread?
> > i am not trying to obtain a lock on it, just trying to access it.
> 
> If you first tell us haw you actually lock a variable, we then might be able
> to tell you what happens if you access it....
> 
> And in general: python has the PIL - Python Interpreter Lock - that

I think you mean the GIL (Global Interpreter Lock).  PIL is the
excellent Python Imaging Library.

> "brutally" serializes (hopefully) all accesses to python data-structures -

Nope.  It doesn't do this.  For access to items such as integers you are
probably fine, but for things like lists, dictionaries, class
attributes, etc, you're on your own.  The GIL only ensures that two
threads won't be executing Python bytecode simultaneously.  It locks the
Python *interpreter*, not your program or data structures.

> so e.g. running several threads, appending to the same list, won't result
> in messing up the internal list structure causing segfaults or the like.

True, you won't get segfaults.  However, you may very well get a
traceback or mangled data.

> That makes programming pretty easy, at the cost of lots of waiting for the
> individual threads.

Threading in Python is pretty easy, but certainly not *that* easy.  And
just to be certain, importing PIL won't help you here either <wink>.

Regards,
Cliff

-- 
Cliff Wells <clifford.wells at comcast.net>




More information about the Python-list mailing list