1

I have script1.py which does some calculations and sends some emails and script2.py (is used to do some updated in a Mysql DB) which is in an infinite loop until some things are satisfied.

At the end of the calculations made on script1, I need to execute script2.py and to do this I am using subprocess.Popen(["python", "script2.py"]) now when debugging this, I see that it goes inside script2 and it works but when the execution of script1 ends I see that the script2.py stops its execution.

Is there another module used to do this kind of things, because it's not working properly ?

EDIT: I need to execute script1.py in order to execute script2.py but when using subprocess.Popen() the script1 waits for the script2 to terminate the execution. I need a way to execute script2 and let it run and terminate script1

strangethingspy
  • 246
  • 1
  • 2
  • 13
  • 1
    Possible duplicate of [Python subprocess.Popen() wait for completion](https://stackoverflow.com/questions/28284715/python-subprocess-popen-wait-for-completion) – FlyingTeller Mar 19 '19 at 12:05
  • You need to specifically wait until the command run with `popen` has completed. See [this](https://stackoverflow.com/questions/28284715/python-subprocess-popen-wait-for-completion) – FlyingTeller Mar 19 '19 at 12:06
  • Why dont you `import script2` and call it directly? – balderman Mar 19 '19 at 12:07
  • I don't think this is the same as the one referenced by +FlyingTeller, this one seems more appropriate but it depends what Script2 is doing: https://stackoverflow.com/questions/27624850/launch-a-completely-independent-process – Simon Hibbs Mar 19 '19 at 12:16
  • @FlyingTeller I need to start an independent process and subprocess does not do that – strangethingspy Mar 19 '19 at 13:37

1 Answers1

1

script_1.py:

import subprocess
process = subprocess.Popen(["python", "script_2.py"])
print("CONTROLLER TERMINATED")

script_2.py:

import time
for x in range(5):
    print("ALIVE")
    time.sleep(1)

enter image description here

After adding in process.wait() to script_1.py:

import subprocess
process = subprocess.Popen(["python", "script_2.py"])
process.wait()
print("CONTROLLER TERMINATED")

enter image description here

Zak Stucke
  • 432
  • 6
  • 18