[Tutor] : Threads?

Kent Johnson kent37 at tds.net
Fri Oct 21 21:03:56 CEST 2005

Orri Ganel wrote:
> Hello all,
> I've been working on a program for a week or two now that will convert 
> all the wav files in a folder to mp3s, filling the id3 tags with the 
> correct information as collected from gracenote.com.  This part works 
> fine.  However, the actual conversion to mp3 takes between 30 and 50 
> seconds per song, so it's usually about 10 minutes per album.  With this 
> in mind, I thought, why not try to use threads so all the conversions 
> happen simultaneously?  That way, the whole album will take between 30 
> and 50 seconds.  Unfortunately, I can't seem to get a working threaded 
> version that significantly reduces the time involved . . . The 

The only part you are doing in a thread is the actual conversion. This is likely to be CPU-intensive so running it in multiple threads may not help - you still have only the one CPU to run on. To the extent that you can overlap disk I/O in one conversion with processing in another you may get a win; on the other hand you could just as well have contention for the disk as you try to read and write a bunch of files at the same time.

The fetch from gracenote.com seems like a better candidate for threading because there is some latency...but the total time is still probably small compared to the conversion time.

Maybe if you have multiple CPUs you can get a speedup by using as many threads as CPUs...I'm not sure how os.system() behaves in this case. You may have to explicitly fork to get a new process.

Hmm...come to think of it, os.system() may block other threads, I don't know...you could try subprocess.Popen() instead.


> unthreaded version is available at 
> http://rafb.net/paste/results/Y1DTnW54.html, and the current, only 
> slightly time-improved threaded version is available at 
> http://rafb.net/paste/results/Lvsjj495.html. Any and all suggestions as 
> to how to threadify this are welcome.  I realize the code isn't as 
> pretty as it could be, but first I want to make a threaded version that 
> works.  Once this has been accomplished, I'll finish commenting the code 
> and making it more user-friendly to read.
> Thanks in advance,
> Orri
> P.S. - In order to make my code work, it is necessary to download 
> several modules. Once the code works, I'll include above the code a 
> comment listing all the requirements. Right now, I believe the following 
> modules are needed:
> ClientForm - http://wwwsearch.sourceforge.net/ClientForm/
> id3writer - 
> http://www.comfortableshoe.co.uk/cgi-bin/blosxom.cgi/Home/Python/id3Writer.comments 
> (click the "download link" link)
> id3reader - 
> http://www.comfortableshoe.co.uk/cgi-bin/blosxom.cgi/Home/Python/id3Writer.comments 
> (click the "Ned Batchelder's id3Reader" link)
> PyID3 - http://sourceforge.net/projects/pyid3
> threadpool - http://chrisarndt.de/en/software/python/threadpool.html
> I also considered using the following, but decided not to for reasons of 
> difficulty of use or lack of desired operations:
> PyID3Tag - http://superduper.net/?page=pyid3tag
> ID3-Py - http://id3-py.sourceforge.net/
> The following non-Python program is also needed:
> Lame - http://lame.sourceforge.net/download/download.html

More information about the Tutor mailing list