[Stackless] Mixing os threads and stackless

Lars Immisch lars at ibp.de
Wed Apr 26 15:38:10 CEST 2006


Hi Johan,

>> You are correct. The GIL only allows one thread to be executing *python 
>> code*, in the sense of: interpreting python bytecode, instead of: 
>> calling a library function that calls out to C underneath (and releases 
>> the GIL).
>>
>> But this is a subtle distinction that requires understanding what is 
>> implemented in terms of C functions and what isn't.
>>
>> Now, if anyone mixes OS threads and tasklets, s/he should have that 
>> understanding or go acquire it.
> 
> That's the kind of understanding I'm seeking here :-)
> 
> But at the moment I'm confused. So I hope you guys can help me get this 
> right.
> 
> Maybe I'm confused by what "only thread executing *python code* at once"
> means?

Probably. I'll repeat myself, because I can't explain it more concisely:

The GIL only allows one thread to be executing *python code*, in the 
sense of: interpreting python bytecode, instead of: calling a library 
function that calls out to C underneath (and releases the GIL).

If you want to know which functions are implemented in terms of pure 
python code, and which ones are implemented in C, you'd have to look at 
its implementation if you want to be 100% sure.

But as a rule of thumb, everything that does waiting for I/O is 
typically implemented in C and releases the GIL.

So sleep(), read(), write() select(), poll(), recv(), send() are all C 
functions.

Database wrappers like cx_Oracle or what have you are also implemented 
in terms of C functions, so a cursor.execute() will also release the GIL.

Unless you use multithreaded programming on a multiprocessor machine for 
parallel numerical computations, you will most likely use a 
multithreaded design because you need to multiplex I/O.

And in this case, the GIL won't be in your way and multiple OS threads 
will really run concurrently.

> To me this means that two threads can't run python code (from the same 
> interpretor) on separate CPUs at the same time.
> On a single processor (at least in my mind) no code (python nor c) can 
> run in at once!!?

No and no.

- Lars

_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless



More information about the Stackless mailing list