Threading In Python

Ian Kelly ian.g.kelly at gmail.com
Thu Dec 12 03:41:51 EST 2013


On Thu, Dec 12, 2013 at 1:08 AM, marcinmltd <marcinmltd at gmail.com> wrote:
> Adding subject to the message.
> Hello,
>
> I'm big fan of multiprocessing module, but recently I started looking at
> threading in Python more closely and got couple of questions I hope You can
> help me with:
>
> 1. When I run two or more threads in my python process are they really run
> concurrently on mulicore machine?

No.  The GIL allows the Python interpreter to run in only thread at a time.

> 2. Browsing through documentation it looks like python interpreter protects
> its sensitive states by using GIL. Can you guys list situations when this
> happens?

Any time Python code is being run, the GIL is held.  C extensions have
the option to release the GIL for long-running operations (e.g.
waiting on a network socket), but they are not permitted to work with
Python objects while the GIL is released.

> 2. What would be general advice from python experts on when to use threadng
> and when switch to multliprocessing in python? Is the decision still
> influenced by how often we need to comunicate between the tasks as it's in
> C\C++?

Generally speaking, use threading for programs that are IO-bound
(where there would be little concurrency anyway) and multiprocessing
for programs that are CPU-bound.  Communication between processes is
more expensive than communication between threads, so that could be an
important criterion.



More information about the Python-list mailing list