6

I'm using Python 3.4.2 on Windows. In script1.py I'm doing this:

myProc = subprocess.Popen([sys.executable, "script2.py", "argument"])
myProc.communicate()

it works and call script2.py . The problem is that in script2.py there is a infinite loop (there must be) and the script1.py is waiting for script2.py to finish. How can I tell to script1.py to just call script2.py and don't wait for the process to finish?

jul
  • 467
  • 3
  • 5
  • 12
  • Does this help? http://stackoverflow.com/questions/13243807/popen-waiting-for-child-process-even-when-the-immediate-child-has-terminated – nekomatic Jan 30 '15 at 09:19
  • Unrelated: as a more flexible alternative, you could import the script as a Python module instead (use `if __name__ =="__main__"` guard in it, to avoid running the code on import) and call functions directly or using `threading`, `multiprocessing`, `concurrent.futures` if needed. – jfs Jan 30 '15 at 14:14

2 Answers2

14

Just don't call myProc.communicate() if you don't want to wait. subprocess.Popen will start the process.

mavroprovato
  • 8,023
  • 5
  • 37
  • 52
2

Call the script in another window.

myProc = subprocess.Popen(["start", sys.executable, "script2.py", "argument"])
myProc.communicate()

start is a windows shell function that runs a program separately, allowing the current one to continue its process. I haven't tested this as I've no access to a Windows OS, but The linux equivalent (nohup) works as required.

If you need fine control over what happens with script2.py, refer to the multiprocessing module here.

Iskren
  • 1,301
  • 10
  • 15