calling a program without waiting for it to finish

Alex Martelli aleax at aleax.it
Thu Mar 20 14:15:06 EST 2003


On Thursday 20 March 2003 06:07 pm, Brian Munroe wrote:
> > the shell interactively, only, of course!-) as soon as it's done
> > setting things up, so the pipeline runs in separate processes.
> >
> > os.system delegates the whole operation to the system shell (normally
> > sh) so the above information should be directly applicable...
>
> So if you enter os.system('ping blah.com &') it will basically shell out
> to the os and run the ping command in the background?  If you use ping

Yes, that's the idea.

> without a -n option (atleast on the machine I am on) it will continue
> to ping forever (or until ^C)

That's the Unix standard for the ping command, yes.


> So, do you know if this process terminates when the python script
> finishes, or will it continue to run in the background?  If it continues
> to run, this could cause problems.

In general, the process will continue to run to completion, unless you 
take specific steps to terminate it.  If the program running in the
process is coded to "loop forever", as in the case of ping, then indeed
you could suffer the problems you mention (unless other factors
help, e.g. the terminal window also closing -- the background process
isn't necessarily "daemonized" and so it will probably be killed if its
controlling terminal closes).

You can take specific, platform-dependent action to terminate such
processes in a function run at your Python program's termination
(see standard library module atexit).  E.g., killall will work on some
systems for such purposes.


Alex






More information about the Python-list mailing list