I have a code that spawns a process with subprocess.Popen
:
from subprocess import check_call, CalledProcessError, Popen, PIPE
cmd="while true; do echo 123; done | grep -m1 123"
proc = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
When I run it with Python 3.9.2, it terminates immediately as expected. However when I run it with Python 2.7 it hangs. It seems that Python 2.7 waits for shell infinite loop to terminate but it will not terminate ever. Can I make this code terminate under Python 2.7 as well?