Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61685

Re: Threading In Python

References <abwcw12b22bnt7r1atxo7jwn.1386835706094@email.android.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2013-12-12 01:41 -0700
Subject Re: Threading In Python
Newsgroups comp.lang.python
Message-ID <mailman.3975.1386837754.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Dec 12, 2013 at 1:08 AM, marcinmltd <marcinmltd@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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Threading In Python Ian Kelly <ian.g.kelly@gmail.com> - 2013-12-12 01:41 -0700

csiph-web