Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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; '(even': 0.05; 'interpreter': 0.05; 'that?': 0.05; 'cache': 0.07; 'context': 0.07; 'interpreter.': 0.07; 'subject:help': 0.08; 'back.': 0.09; 'interpreted': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'runtime': 0.09; 'python': 0.11; 'thread': 0.14; '"every': 0.16; '(yes': 0.16; 'blocked': 0.16; 'bytecode': 0.16; 'core.': 0.16; 'processor,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'repetition': 0.16; 'thread,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'trying': 0.19; 'seems': 0.21; "aren't": 0.24; 'processor': 0.24; 'url:home': 0.24; 'helpful': 0.24; 'versions': 0.24; 'switch': 0.26; 'gets': 0.27; 'header:X -Complaints-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; "i'm": 0.30; 'code': 0.31; 'overhead': 0.31; 'running': 0.33; 'core': 0.34; 'subject:with': 0.35; 'common': 0.35; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'done,': 0.36; 'done': 0.36; "didn't": 0.36; 'charset:us-ascii': 0.36; 'so,': 0.37; 'level': 0.37; 'implement': 0.38; 'received:76': 0.38; 'ahead': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'skip:u 10': 0.60; 'easy': 0.60; 'catch': 0.60; 'lower': 0.61; 'new': 0.61; 'making': 0.63; 'more': 0.64; 'different': 0.65; 'due': 0.66; 'default': 0.69; '100': 0.79; 'transfer': 0.82; 'misses': 0.84; 'overall,': 0.84; 'carlos': 0.91; 'same,': 0.91; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: Please help with Threading Date: Sun, 19 May 2013 17:46:11 -0400 Organization: > Bestiaria Support Staff < References: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com> <13lfp8lds6e2e41rtsnvqimcb6inu7p28o@invalid.netcom.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-249-17-62.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES 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: 42 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368999978 news.xs4all.nl 15910 [2001:888:2000:d::a6]:40882 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45566 On Sun, 19 May 2013 10:38:14 +1000, Chris Angelico declaimed the following in gmane.comp.python.general: > On Sun, May 19, 2013 at 10:02 AM, Carlos Nepomuceno > wrote: > > I didn't know Python threads aren't preemptive. Seems to be something really old considering the state of the art on parallel execution on multi-cores. > > > > What's the catch on making Python threads preemptive? Are there any ongoing projects to make that? > > With interpreted code eg in CPython, it's easy to implement preemption > in the interpreter. I don't know how it's actually done, but one easy > implementation would be "every N bytecode instructions, context > switch". It's still done at a lower level than user code (N bytecode Which IS how the common Python interpreter does it -- barring the thread making some system call that triggers a preemption ahead of time (even time.sleep(0.0) triggers scheduling). Forget if the default is 20 or 100 byte-code instructions -- as I recall, it DID change a few versions back. Part of the context switch is to transfer the GIL from the preempted thread to the new thread. So, overall, on a SINGLE CORE processor running multiple CPU bound threads takes a bit longer just due to the overhead of thread swapping. On a multi-core processor, the effect is the same, since -- even though one may have a thread running on each core -- the GIL is only assigned to one thread, and other threads get blocked when trying to access runtime data structures. And you may have even more overhead from processor cache misses if the a thread gets assigned to a different core. (yes -- I'm restating the same thing as I had just trimmed below this point... but the target is really the OP, where repetition may be helpful in understanding) -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/