Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'exercise': 0.04; 'cpython': 0.05; 'interpreter': 0.05; 'say,': 0.05; 'subject:Python': 0.06; '*not*': 0.07; 'concurrently': 0.07; 'pypy': 0.07; 'think,': 0.07; 'differently.': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'language.': 0.14; '(there': 0.16; 'behave': 0.16; 'c-level': 0.16; 'do)': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'jython,': 0.16; 'library),': 0.16; 'machine?': 0.16; 'numpy': 0.16; 'syntax,': 0.16; 'threads,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'module': 0.19; 'trying': 0.19; 'else,': 0.19; 'thu,': 0.19; 'python?': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'beauty': 0.24; 'closely': 0.24; 'instance,': 0.24; 'module,': 0.24; 'guys': 0.24; 'together.': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'switch': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'dec': 0.30; 'especially': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'run': 0.32; 'cases': 0.33; 'problem': 0.35; 'advice': 0.35; 'something': 0.35; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; "i'll": 0.36; 'two': 0.37; 'list': 0.37; 'tasks': 0.38; 'pm,': 0.38; 'expect': 0.39; '12,': 0.39; 'enough': 0.39; 'mailing': 0.39; 'release': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'experts': 0.60; 'most': 0.60; 'hope': 0.61; "you're": 0.61; 'first': 0.61; 'discuss': 0.62; 'here:': 0.62; 'more': 0.64; 'different': 0.65; 'talking': 0.65; 'between': 0.67; 'close': 0.67; 'advantages': 0.68; 'real-world': 0.68; 'difference.': 0.84; 'etc,': 0.84; 'execution.': 0.84; 'protects': 0.84; 'browsing': 0.91; 'to:none': 0.92; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=QVrwJjYuIn6ixOMMR+/6nLpwbn4BQF9KqIjAUX+IlfM=; b=hSYcEAXgxivE9nbn4V9dGOmuKCiVRX2UulLEcz42Qym5WQSaWX9G7058kNgB26duOJ gjtkWZqgzQhjBgWGwfAov6pg5VHeujNJESpTPNfbO1SS6K3g9r5U1T0C3rD+rZiJM+1t hm7nWidjZnp+7avkpuJ6aix3IBpND8VO+JHnzyEc/Ty9TS16iUmx3wa8UXBH9fYGXYAZ rWNBYdgrdSXPa82yG1LK58WyOuQBFkDA0speAyD08zBEMeX1X4dEq3j2nA3hZRCmYLbT qkv+oArd5vKacKW5fkhtwZDcgr7tWYEB///wTY71WQ+nMKUjWlLtk3xgYhNvAiPV3buV QreA== MIME-Version: 1.0 X-Received: by 10.68.236.133 with SMTP id uu5mr9658358pbc.153.1386837581526; Thu, 12 Dec 2013 00:39:41 -0800 (PST) In-Reply-To: References: Date: Thu, 12 Dec 2013 19:39:41 +1100 Subject: Re: Threading In Python From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386837584 news.xs4all.nl 2942 [2001:888:2000:d::a6]:57131 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61684 On Thu, Dec 12, 2013 at 7:08 PM, marcinmltd wrote: > 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: The first thing to note is that there are two completely separate concepts here: 1) Python is a language. It is defined by a set of specifications covering syntax, semantics, etc, etc, etc. 2) CPython, Jython, IronPython, and PyPy are four implementations of the Python language. (There are others, too, but those are the four biggest.) They're all a bit different in how they behave. Most likely, you're talking about CPython, which is the most commonly-used implementation of Python. I'm going to discuss CPython here, but be aware that the other Pythons may well (and in some cases definitely DO) behave differently. > 1. When I run two or more threads in my python process are they really run > concurrently on mulicore machine? > > 2. Browsing through documentation it looks like python interpreter protects > its sensitive states by using GIL. Can you guys list situations when this > happens? I'll answer these two together. Most likely the two threads will *not* run concurrently, because CPython uses the Global Interpreter Lock around all Python code execution. The GIL is released any time there's going to be a long-running C-level call, especially something that waits; so if your threads are spending most of their time, say, trying to read from sockets, then there's no problem and the GIL won't get in your way. There are a very VERY few CPU-bound operations that release the GIL (some numpy calculations, I think, but I'm not familiar with that library), but the general rule of thumb is that whenever the CPU's busy, you own the GIL. > 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++? Use threading when you're waiting for something else, and multiprocessing when you need to exercise multiple CPU cores. Again, that's just a rule of thumb, but it's close enough for a base understanding of the difference. Since I don't have real-world experience with any Python other than CPython, I won't speak to those; but do expect that there'll be differences. (I think, for instance, Jython's threading module is backed by Java's threads, with all the advantages and disadvantages that entails.) But the beauty of this mailing list is that someone else will fill that in :) ChrisA