How to schedule system calls with Python

TerryP bigboss1964 at gmail.com
Thu Oct 15 19:17:49 EDT 2009


On Oct 15, 8:52 pm, Jeremy <jlcon... at gmail.com> wrote:
> On Oct 15, 2:15 pm, TerryP <bigboss1... at gmail.com> wrote:
> > If you want simple sequenceal execution of external programs, use a
> > suitable blocking function to execute them (like system) combined with
> > a simple loop over the sequence of commands to run.
>
> This is the solution I wanted.  I thought that os.system(prog) would
> return immediately regardless of how long prog takes to run.  I should
> have tried this simple solution first.  Thanks for being patient.
>
> Jeremy

launching external programs, irregardless of language, generally falls
into 3 major categories:

  0.) blocks until program is done; like system
  1.) replaces your program with process, never returns; like exec
  2.) quickly return after asynchronously launching the program

Most languages will implement the first method because of the standard
system() function in C, which is fairly popular in it's own right.
Most multi-tasking operating systems will implement some form of exec
function, which Python exposes through the os module. The last method
is the least portable, because obviously if the OS lacks multi-tasking
you're screwed. The best examples of 2. are the UNIX popen() function
and Microsoft's spawn() family, when used with the P_DETACH flag.
Python being made with much loving kindless, exposes each interface.
More system specific functions are exposed through the os module in a
fairly portable way in so much as is actually possible under Unix and
Windows.


The standard subprocess module provides a portable Popen class and a
few convenience functions for all occasions - including good
documentation on how to use it to establish the common goals most
people will ever have, when wishing to invoke an external program. I
personally suggest learning how to use the subprocess module, unless
you may as well be programming in the C or Lisp family ;)


Vive la différence!



More information about the Python-list mailing list