"Do this, and come back when you're done"

Roy Smith roy at panix.com
Sat Dec 13 08:38:27 EST 2003


Kamus of Kadizhar <yan at NsOeSiPnAeMr.com> wrote:
> Is there any way to rewrite each half of the function to run in the 
> background, so to speak, and then have a master process that waits on 
> the results?

Yup.  Two ways in fact.

The traditional way would be to fork another process to do the work and 
have the parent process wait for the child to finish.  You'll need to 
use the fork() and exec() functions that can be found in the os module.

The other way would be to do something similar, but with threads instead 
of processes.  The basic flow is the same; you create a thread, have 
that thread do the stuff that takes a long time, and then rejoin with 
the primary thread.  Of course (just like with child processes), you 
could have multiple of these running at the same time doing different 
parts of a parallelizable job.  Take a look at the Threading module.

I'm intentionally not including any sample code here, because the 
possibilities are numerous.  Exactly how you do it depends on many 
factors.  I'm guessing that doing it with threads is what you really 
want to do, so my suggestion would be to start by reading up on the 
Threading module and playing with some examples to get the feel for how 
it works.  Working with threads is becomming more and more mainstream 
and more operating systems and languages provide support for it, and the 
programming community at large becomes more familiar and comfortable 
with the issues involved.




More information about the Python-list mailing list