Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!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; 'scripts': 0.03; 'interpreter': 0.05; 'nested': 0.07; 'subject:help': 0.08; '(instead': 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; 'works.': 0.09; 'runs': 0.10; 'python': 0.11; 'thread': 0.14; 'conflicting': 0.16; 'dict': 0.16; 'dictionaries': 0.16; 'interpreter,': 0.16; 'iterating': 0.16; 'loops': 0.16; 'numpy': 0.16; 'processor,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'thread,': 0.16; 'threads.': 0.16; 'elements': 0.16; 'prevent': 0.16; 'sat,': 0.16; 'library': 0.18; 'trying': 0.19; 'starts': 0.20; 'seems': 0.21; 'code,': 0.22; 'creating': 0.23; 'instead.': 0.24; 'url:home': 0.24; 'fine': 0.24; 'script': 0.25; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'array': 0.29; 'thus': 0.29; 'code': 0.31; 'complete,': 0.31; 'long.': 0.31; 'overhead': 0.31; 'lists': 0.32; 'this.': 0.32; 'run': 0.32; 'another': 0.32; 'up.': 0.33; 'mac': 0.33; 'actual': 0.34; 'core': 0.34; 'subject:with': 0.35; 'common': 0.35; 'created': 0.35; 'something': 0.35; 'really': 0.36; 'module.': 0.36; 'doing': 0.36; 'next': 0.36; 'charset:us-ascii': 0.36; 'list': 0.37; 'level': 0.37; 'list.': 0.37; 'being': 0.38; 'received:76': 0.38; 'to:addr :python-list': 0.38; 'resource': 0.38; 'that,': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'release': 0.40; 'even': 0.60; 'devices': 0.61; 'first': 0.61; "you'll": 0.62; 'between': 0.67; 'sharing': 0.69; 'intelligent': 0.74; 'goal': 0.75; 'gain': 0.79; '(while': 0.84; '80%': 0.84; 'multicore': 0.84; 'odds': 0.84; 'drops': 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: Sat, 18 May 2013 15:28:56 -0400 Organization: > Bestiaria Support Staff < References: <7baacf5a-0c50-4935-ad5b-148c208d759b@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-249-28-99.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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368905346 news.xs4all.nl 15951 [2001:888:2000:d::a6]:53125 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45525 On Sat, 18 May 2013 01:58:13 -0700 (PDT), Jurgens de Bruin declaimed the following in gmane.comp.python.general: > This is my first script where I want to use the python threading module. I have a large dataset which is a list of dict this can be as much as 200 dictionaries in the list. The final goal is a histogram for each dict 16 histograms on a page ( 4x4 ) - this already works. > What I currently do is a create a nested list [ [ {} ], [ {} ] ] each inner list contains 16 dictionaries, thus each inner list is a single page of 16 histograms. Iterating over the outer-list and creating the graphs takes to long. So I would like multiple inner-list to be processes simultaneously and creating the graphs in "parallel". > I am trying to use the python threading for this. I create 4 threads loop over the outer-list and send a inner-list to the thread. This seems to work if my nested lists only contains 2 elements - thus less elements than threads. Currently the scripts runs and then seems to get hung up. I monitor the resource on my mac and python starts off good using 80% and when the 4-thread is created the CPU usages drops to 0%. > The odds are good that this is just going to run slower... One: The common Python implementation uses a global interpreter lock to prevent interpreted code from interfering with itself in multiple threads. So "number cruncher" applications don't gain any speed from being partitioned into thread -- even on a multicore processor, only one thread can have the GIL at a time. On top of that, you have the overhead of the interpreter switching between threads (GIL release on one thread, GIL acquire for the next thread). Python threads work fine if the threads either rely on intelligent DLLs for number crunching (instead of doing nested Python loops to process a numeric array you pass it to something like NumPy which releases the GIL while crunching a copy of the array) or they do lots of I/O and have to wait for I/O devices (while one thread is waiting for the write/read operation to complete, another thread can do some number crunching). If you really need to do this type of number crunching in Python level code, you'll want to look into the multiprocessing library instead. That will create actual OS processes (each with a copy of the interpreter, and not sharing memory) and each of those can run on a core without conflicting on the GIL. -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/