2

I have a program I'm running within another. The parent program freezes up while the child process is running. Is there a way to run the child process in the OS as a parent process itself?

john
  • 408
  • 9
  • 30
  • What you want is a separate thread – keyser Jul 16 '14 at 17:33
  • 1
    When you say you have programs running "within" one another, do you mean one Python script calling another, a Python script launching other programs, a Python script whose functions you want executed in a subprocess, or something else? – skrrgwasme Jul 16 '14 at 17:48
  • Scott: In Maya (3D app) I'm trying to run an external program. The external program 'locks up' Maya when it is running. I get control of Maya when I close the external/child program. Does that help? Sorry, bit of a newb. – john Jul 16 '14 at 18:31

2 Answers2

5

You can use subprocess.Popen, assuming you're really trying to launch a program that's completely separate from the parent Python script:

import subprocess
subprocess.Popen(["command", "-a", "arg1", "-b", "arg2"])

This will launch command as a child process of the calling script, without blocking to wait for it to finish. If the parent exits, the child process will continue to run.

dano
  • 91,354
  • 19
  • 222
  • 219
  • I got this to work outside of a function, but when in a function it doesn't run a detached subprocess. import subprocess def myFunc(): #do stuff subprocess.Popen("C:\myProgram.exe") myFunc() – john Jul 16 '14 at 18:40
  • @john What *does* it do? – dano Jul 16 '14 at 18:45
0

If you really want to have independent processes, please have a look at the multiprocessing module. If it is enough to have a separate thread running in the same OS process, then use threading. Or are you interested in starting an external program from within a Python script with subprocess?

Unfortunately, the terminology is a bit confusing. For example, in Linux "thread" and "process" are both independent processes with no real difference. In python "process" is a separate OS precess, thread runs in the same OS process.

For more information on these, you might have a look at this question: Multiprocessing vs Threading Python

Community
  • 1
  • 1
DrV
  • 22,637
  • 7
  • 60
  • 72