Please help me understand how subprocess.popen works

Hello Python Masters…I am a beginner…Please help me understand how this code works:

PyLocScript = r"C:\Phyton\PyApps\CSV_values_analyze.py"
CsvLink = r"C:\Phyton\PyApps\sample.csv"
PartNo = "LM3339"
area = 45
xcor = 1.5
ycor = 3.5

theproc = subprocess.Popen([sys.executable,PyLocScript,CsvLink,PartNo,str(area), str(xcor), str(ycor)])
theproc.communicate()

I did some research about subprocess.popen but i can’t find an example the same with the code above. But my understanding with popen it will run the PyLocScript. And the area, xcor, ycor are values required by PyLocScript. Please correct me if am wrong. And what theproc.communicate() does? Hope someone can help explain in layman’s term…thanks in advance.

You’re assessment is correct.

theproc.communicate() is a little odd, but it transfers data to and from the subprocess. It’s used to help deter deadlocks, but the important thing is just to know that this is how you get I/O to and from the subprocess.

1 Like

Well, it’s a convenient and robust way.

I’d probably point new people to subprocess.run these days. Everything
in one box! It relegates subprocess.Popen to the less common
situations.

Cheers,
Cameron Simpson cs@cskk.id.au

2 Likes

In this case, p.communicate() is basically the same as p.wait(). On the other hand, if stdin, stdout, and stderr are set to subprocess.PIPE, then p.communicate(input) allows writing input to p.stdin and reading from p.stdout and p.stderr – all performed concurrently in order to avoid a deadlock between the parent and child.

2 Likes