Clarity: GIL, processes and CPUs etc

Alan Kennedy alanmk at hotmail.com
Wed Feb 15 12:22:13 EST 2006


[adsheehan at eircom.net]
> I understand that embedding the interpreter in a C/C++ application
> limits it to one CPU.
> If the application is multi-threaded (system threads) in will not use
> additional CPUs as the interpreter is tied to one CPU courtesy of the
> GIL.
> True or False?

True, only when a thread is inside pure python code. C-level extensions
and library modules, e.g. I/O modules, release the GIL, thus permitting
other threads in the same process to run simultaneously. But only one
thread can be running *inside the python interpreter* at a time.

> I understand that forking or running multiple process instances of the
> above application would make use of multiple CPUs. This is because each
> process would have its own interpreter and GIL that is independent of
> any other process.
> True or False?

True. Every separate process will have its own python interpreter,
meaning it has its own GIL. Python code running in multiple processes
can execute truly simultaneously.

So you can run pure python code simultaneously on multiple cpus on a
multi-cpu box by using multiple independent processes. But if your
processes need to communicate, then you need to de/serialise
objects/parameters for transmission between those processes.

--
alan kennedy
------------------------------------------------------
email alan:              http://xhaus.com/contact/alan




More information about the Python-list mailing list