[Tutor] : Threads?

Kent Johnson kent37 at tds.net
Sat Oct 22 15:11:56 CEST 2005


Orri Ganel wrote:
> I'll try doing 3 or 4 tracks per thread, then. Thanks for the advice.
> 
> 
>     If you keep the thread count down to two or three you might get
>     a noticable improvement but one thread per track, unless you have
>     a lot of separate hard disk spindles to distribute the work will
>     not help much I suspect.

Alan means to use two or three threads *total*. 

Orri, I took another look at your program and you are not using threads correctly; the way you have it set up you are just doing one conversion at a time. Your main loop is roughly like this:

for each track
  make a thread pool
  queue a request to convert the track
  wait for all threads to finish (but there is only one)
  finish converting the track

Since you wait for each thread individually, the processing ends up being sequential.

You should put all the processing for a track in convert_thread and structure your code like this:

make a thread pool with 2-3 threads
for each track
  queue a request to convert the track
wait for all threads to finish
finish up - processing that has to be done after all threads complete

With this structure, you will always be running 2-3 conversions at a time; when one finishes you will start another until they are all done. You might want to print 'starting' and 'finishing' messages in convert_thread so you can see the overlap.

When you get it working, experiment with the number of threads in the pool to see what number gives the best performance.

============================

I wanted to find out whether os.system() calls block other threads. It seems that they don't. (That's a good thing, it means it should work for you!) Here is a program to test this:

# ThreadSystem.py
import os, threading

def makeCommand(n):
    return '''python -c "import time;print 'hello   %s',time.time();time.sleep(2);print 'goodbye %s',time.time()"''' % (n, n)

threading.Thread(target=os.system, args=(makeCommand(1),)).start()
threading.Thread(target=os.system, args=(makeCommand(2),)).start()


On my Win2K machine it consistently prints results like
F:\Tutor>python threadsystem.py
hello   1 1129985497.16
hello   2 1129985497.17
goodbye 1 1129985499.16
goodbye 2 1129985499.17

so the two threads are both active.

Kent



More information about the Tutor mailing list