2

I have a python script script_a.py that uses subprocess.call() that executes another script script_b.py as it's last instruction before ending. I need script_b.py to wait until script_a.py closes before proceeding with its own instructions. For this I am using a while loop within script_b.py. How can I do this? All of my current solutions I have tried have script_a.py waiting until script_b.py is finished before it closes itself. I have a feeling this might involve atexit() or something similar, but I am lost.

Many thanks!

3
  • it sounds like you are using the wrong construct ... I think you want os.exec* instead of subprocess ... Commented Jul 21, 2016 at 16:35
  • Got it. In the event that script_b.py has everything it needs to run defined within itself, then what am I expected to pass in the list of args that the exec* functions force me to pass? Commented Jul 21, 2016 at 16:52
  • you can probably just pass the ["python","script_b.py"] ... its been a while since i have done that ... Commented Jul 21, 2016 at 18:03

2 Answers 2

1

you could make some totally hacky crap

script_b.py

while not os.path.exists("a.done"):pass
time.sleep(0.2) # a little longer just to be really sure ...
os.remove("a.done")
... # rest of script b

script_a.py

import atexit
atexit.register(lambda *a:open("a.done","w"))

or instead of Popen just do

os.execl("/usr/bin/python","script_b.py")
Sign up to request clarification or add additional context in comments.

Comments

1

Your script_a.py would be:

import subprocess
#do whatever stuff you want here
p = subprocess.Popen(["python","b.py"])
p.wait()
p.terminate()

#continue doing stuff

4 Comments

Where does this close script_a.py so the subprocess p can do its work?
Sorry I read it the other way around. If a calls b in the last instruction, and b needs to wait until a finishes you have an infinite loop. Just use another script c that calls a and then b. Any reason why you can't do that?
So there is no way to force script_a to continue on with its next instruction (which is sis.exit() regardless of when script_b finishes?
Use a script c that calls both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.