Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'cpython': 0.05; 'python)': 0.05; 'threads,': 0.07; 'finished,': 0.09; 'am,': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'semaphores': 0.16; 'subject:threads': 0.16; 'thread.': 0.16; 'workers.': 0.16; 'wrote:': 0.18; 'received:209.85.210.174': 0.18; 'received:mail-iy0-f174.google.com': 0.18; 'convert': 0.19; 'module,': 0.23; 'header:In-Reply-To:1': 0.23; 'starts': 0.23; 'command': 0.24; 'specifically': 0.27; 'script': 0.28; 'message- id:@mail.gmail.com': 0.29; 'problem': 0.29; 'fairly': 0.30; 'separated': 0.30; 'threads.': 0.30; '(the': 0.30; 'url:library': 0.30; 'nov': 0.31; 'version': 0.31; 'thu,': 0.32; 'pretty': 0.32; 'to:addr:python-list': 0.32; '17,': 0.34; 'url:python': 0.35; 'thread': 0.36; 'another': 0.36; 'using': 0.37; 'run': 0.38; 'received:google.com': 0.38; 'hello,': 0.38; 'received:209.85': 0.38; 'url:org': 0.38; 'finished': 0.38; 'url:docs': 0.38; 'should': 0.39; 'help': 0.39; 'tasks': 0.39; "it's": 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'release': 0.40; 'files': 0.40; 'received:209': 0.40; 'being': 0.40; 'more': 0.60; '2011': 0.62; 'here': 0.65; '12:48': 0.84; 'eduardo': 0.84; '\xa0but': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=xrjMWljlMQQnQfISV8jQG6wHkjG+h1k+rbR7Lz9d1Nc=; b=LS7w0BftcKV6+dKICItfbodaRA0TyAJZpjK/m0G0H2p53DOBQTAUHKVsNfp3Bklj3V sY8+uQ4drvZ6Vf4tCaLEEg8N0j0gl2ysY83VW8CcTLqki50pCHb8gQk7fhX1O3w83RvC Sw3wByw+tzcULxcGaf38IKkhU4tl7DC+ct+vk= MIME-Version: 1.0 In-Reply-To: <31766634.4.1321451296410.JavaMail.geo-discussion-forums@yqcm23> References: <31766634.4.1321451296410.JavaMail.geo-discussion-forums@yqcm23> Date: Thu, 17 Nov 2011 00:55:40 +1100 Subject: Re: Multiple threads From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1321451743 news.xs4all.nl 6959 [2001:888:2000:d::a6]:41203 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15771 On Thu, Nov 17, 2011 at 12:48 AM, Eduardo Oliva wrote: > Hello, I have a py script that reads for all "m2ts" video files and conve= rt them to "mpeg" using ffmpeg with command line. > > What I want to do is: > > =A0I need my script to run 2 separated threads, and then when the first h= as finished, starts the next one....but no more than 2 threads. > =A0I know that Semaphores would help with that. > =A0But the problem here is to know when the thread has finished its job, = to release the semaphore and start another thread. First off, it's better in CPython (the most popular Python) to use multiple processes than multiple threads. That aside, what you're looking at is a pretty common model - a large number of tasks being served by a pool of workers. Have a look at the multiprocessing module, specifically Pool: Version 2: http://docs.python.org/library/multiprocessing.html Version 3: http://docs.python.org/py3k/library/multiprocessing.html Should be fairly straightforward. ChrisA