spawning a separate process

Peter Hansen peter at engcorp.com
Tue Apr 1 15:47:40 CEST 2003


Mark Light wrote:
> 
>        I have a python program from which I wish to spawn an os.system call
> (a bash script) that may take several minutes to run - whilst the rest of
> the script carries on. I have tried using bits of code from books - but it
> never seem to work quite right

In addition to Pádraig's response, here's another possibility:

import threading
class RunInBackground(threading.Thread):
    def __init__(self, command):
        threading.Thread.__init__(self)
        self.command = command
        self.start()
        self.done = False

    def run(self):
        import os
        os.system(self.command)
        self.done = True

Then do something like:

bgThread = RunInBackground('/bin/bash mycommand')

Using the popen approach is no doubt better if you don't have
"special needs", but this might make it easier for you to 
extend the functionality if you decide you need more than what
you originally asked for.

-Peter




More information about the Python-list mailing list