Parallelization in Python 2.6

sturlamolden sturlamolden at yahoo.no
Wed Aug 19 14:56:21 CEST 2009


On 19 Aug, 05:16, sturlamolden <sturlamol... at yahoo.no> wrote:

> You should know about the GIL. It prevents multiple threads form using
> the Python interpreter simultaneously. For parallel computing, this is
> a blessing and a curse. Only C extensions can release the GIL; this
> includes I/0 routines in Python's standard library. If the GIL is not
> released, the C library call are guaranteed to be thread-safe.
> However, the Python interpreter will be blocked while waiting for the
> library call to return. If the GIL is released, parallelization works
> as expected; you can also utilise multi-core CPUs (it is a common
> misbelief that Python cannot do this).


Since I am at it, this is how the GIL can be released:

- Many functions in Python's standard library, particularly all
blocking i/o functions, release the GIL.

- In C or C++ extensions, use the macros Py_BEGIN_ALLOW_THREADS and
Py_END_ALLOW_THREADS.

- With ctypes, functions called from a cdll release the GIL, whereas
functions called from a pydll do not.

- In f2py, declaring a Fortran function threadsafe in a .pyf file or
cf2py comment releases the GIL.

- In Cython or Pyrex, use a "with nogil:" block to execute code
without holding the GIL.


Regards,
Sturla Molden




























More information about the Python-list mailing list