user6035995
user6035995

Reputation:

Bash is waiting for a python subprocess

I have three scripts which call each other. They are the followings:

script1.sh:

s=`./script2.py`;
echo "DONE";

script2.py:

#!/usr/bin/env python3
import subprocess

subprocess.Popen(["./script3.py"])
print ("Exit script2")

script3.py:

#!/usr/bin/env python3
import time

time.sleep(20)
print ("child OK")

Unfortunatelly the script1.sh is a third-party software and I cannot modify that.

The script2.py launch the script3.py in the background and exit. It works well from the command line. But when the the script1.sh call the script2.py that is waiting for the script3.py. So, the script1.sh is freezing. How can I avoid it?

Upvotes: 2

Views: 65

Answers (1)

John1024
John1024

Reputation: 113844

The problem is that script1.sh is capturing stdout and python has not finished writing to stdout until script3.py finishes.

One solution is to send the stdout of script3.py somewhere else. For example, this allows script1.sh to complete quickly:

$ cat script2.py 
#!/usr/bin/env python3
import subprocess
import sys

subprocess.Popen(["./script3.py"], stdout=sys.stderr)
print ("Exit script2")

With this change, script1.sh exits quickly and, much later, child OK appears on the terminal.

Upvotes: 2

Related Questions