Python: fork, pipe and exec

Aki picture Aki · Feb 9, 2012 · Viewed 11.8k times · Source

I want to execute a program in a python application, it will run in the background but eventually come to the foreground.

A GUI is used to interact with it. But controls are offered via a console on stdin and stdout. I want to be able to control it using my application's GUI, so my first idea was:

  • Fork
  • in the parent, dup2 stdin and stdout in order to access them
  • exec the child

Is this easily implementable in python and how? Are there alternative ways to achieve what I want, what would that be?

Answer

Bren picture Bren · Feb 9, 2012

This is reasonably easy using the standard Python subprocess module:

http://docs.python.org/py3k/library/subprocess.html