multi-threaded *nix program needs to spawn an external program/script

arbalt at yahoo.com arbalt at yahoo.com
Wed Feb 6 17:17:29 EST 2002
Would it make sense to add a function to python for *nix systems
    that would do the "start an executable file as a new process"?

If you have a multi-threaded program and some of the threads sometimes
need to spawn off new external programs or script.

You can do the following under win32, which starts the command
in a new process without forking():

    win32api.WinExec(command, ...)

It works great.

However, *nix systems don't have the "Start an executable file as a
new process" system call (just fork/exec).

I've tried various alternatives, which aren't working, such as:
    * os.system() works for a while, but:
          Eventually, at least on solaris, extra copies of the
          parent process are left hanging around and never go
          away (they are not zombies - the zombies are being
          reaped).

    * python code doing the os.fork() + os.exec().

      This is the least reliable of all:  It leaves around lots
      of extra forked() but not defunct/zombie processes.

      With threads, if you're going to fork + exec, you're not supposed to
      do anything between the fork + exec or you're asking for trouble.

      Trying the os.fork() + os.exec() in an interpreted program is
      probably asking for trouble (and sure enough, trouble happens).

>From what I've noticed, read, and heard, fork() inside threads() is
basically a bad idea.  Different OSes have different ideas,
bugs, etc.

But people are bound to need to have threaded programs spawn off separate
processes to do something in *nix.  (We are in that situation.)

What is the solution?  Would it make sense to add a function to python
for *nix systems that would do the "start an executable file as a
new process" that would work on at least *nix systems?

Or is there another solution?

Thanks!



More information about the Python-list mailing list