Threads vs subprocesses

Dave Angel d at davea.name
Fri Jun 15 11:51:01 EDT 2012


On 06/15/2012 09:49 AM, John O'Hagan wrote:
> I have a program in which the main thread launches a number of CPU-intensive
> worker threads. For each worker thread two python subprocesses are started,
> each of which runs in its own terminal: one displays output received from the
> worker thread via a socket, the other takes text input to control the thread
> (this is done by having another thread receive the input via a socket and write
> to the worker thread's arguments).
>
> So far so good, but it occurred to me that instead of launching all these
> worker threads, I could just put their target code in separate executable
> files and launch them as subprocesses. That way there would be fewer threads,
> but each subprocess would be doing more work. 
>
> My question is, on a single core machine, what are the pros and cons of
> threads vs subprocesses in a setup like this? 
>
> Thanks,
>
> John

Two key phrases in your message;   CPU-intensive,  
single-core-machine.  If these have the conventional meaning, you're
better off doing all the processing in your main executable, without
threads at all.

Neither threads nor separate process speed up CPU-intensive execution on
a single-core machine.

Now, your single core is probably hyper-threaded, so you MIGHT get some
improvement from one extra thread.

Or you might have other reasons for multiple threads, such as ease of
dealing with multiple processes.  But if you let them compete for a
single core, they'll slow things down.

DaveA


-- 

DaveA




More information about the Python-list mailing list