Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3a.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.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'interpreter.': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'thread': 0.14; '(use': 0.16; 'dma': 0.16; 'etc...': 0.16; 'interrupted.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'scientists': 0.16; 'subject:audio': 0.16; 'subject:threads': 0.16; 'threads.': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'basically': 0.19; 'written': 0.21; 'issue.': 0.22; 'header:User-Agent:1': 0.23; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'work.': 0.31; '(which': 0.31; 'received:138': 0.31; 'handled': 0.32; 'another': 0.32; 'quite': 0.32; 'up.': 0.33; 'running': 0.33; '(e.g.': 0.33; 'older': 0.33; 'case,': 0.35; 'but': 0.35; 'doing': 0.36; 'subject:?': 0.36; 'two': 0.37; 'audio': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'most': 0.60; 'here': 0.66; 'sample': 0.67; 'believe': 0.68; 'computers': 0.72; 'miss': 0.74; 'subject:Using': 0.84; 'controller': 0.91; 'interrupted': 0.96 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Sturla Molden Subject: Re: Using threads for audio computing? Date: Wed, 14 May 2014 16:23:57 +0200 References: <536f869c$0$2178$426a74cc@news.free.fr> <536f99eb$0$2109$426a74cc@news.free.fr> <53705d34$0$2374$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 138.124.94.181 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 In-Reply-To: <53705d34$0$2374$426a74cc@news.free.fr> 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: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1400077460 news.xs4all.nl 2928 [2001:888:2000:d::a6]:42241 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71562 On 12/05/14 07:33, lgabiot wrote: > But AFAIK the python GIL (and in smaller or older computers that have > only one core) does not permit true paralell execution of two threads. I > believe it is quite like the way multiple processes are handled by an OS > on a single CPU computer: process A has x CPU cycles, then process B has > y CPU cycles, etc... Python threads are native OS threads. The GIL serializes access to the Python interpreter. If your thread is waiting for i/o or running computations in C or Fortran (e.g. with NumPy), it does not need the Python interpreter. Scientists and engineers use Python threads for "true parallel processing" all the time. The FUD you will find about the GIL is written by people who don't fully understand the issue. > So in my case, I must have a way to make sure that: > thread 1 (which gets audio from Pyaudio and put() it in the Queue) is > not interrupted long enough to miss a sample. Here you are mistaken. The DMA controller takes care of the audio i/o. Your audio acquisition thread is asleep while its buffer fills up. You don't miss a sample because your thread is interrupted. You do, however, have to make sure your thread don't block on the write to the Queue (use block=False in the call to Queue.put), but it is not a "GIL issue". In your case you basically have on thread waiting for the DMA controller to fill up a buffer and another doing computations in NumPy. Neither needs the GIL for most of their work. If you are worried about the GIL you can always use processes (multiprocessing, subprocess, or os.fork) instead of threads. Sturla