Binabik
Binabik

Reputation: 1073

How to properly finish a (piped) python subprocess

I have this litte example code snippet which basically does a ls | grep foo with python subprocesses:

import subprocess
import warnings

warnings.simplefilter("always")

lhs = subprocess.Popen(["ls"], stdout=subprocess.PIPE)
rhs = subprocess.Popen(["grep", "foo"], stdin=lhs.stdout)
lhs.stdout.close()
rhs.communicate()

At the end, I get a warning that says:

"/usr/lib/python3.8/subprocess.py:942: ResourceWarning: subprocess 1519 is still running"

Since my code is equivalent to https://docs.python.org/3.8/library/subprocess.html#replacing-shell-pipeline, I am quite sure to do the right thing. So why is this warning generated or what can I do to prevent python from complaining there (besides suppressing warnings)?

Upvotes: 0

Views: 200

Answers (1)

RafalS
RafalS

Reputation: 6324

You're missing lhs.communicate()lhs.wait() (communicate also waits for the process to finish, but wait is more self explanatory),

lhs = subprocess.Popen(["ls"], stdout=subprocess.PIPE)
rhs = subprocess.Popen(["grep", "foo"], stdin=lhs.stdout)
lhs.stdout.close()
rhs.communicate()
lhs.wait()

but IMO you should use higher level functions like subprocess.run

Upvotes: 1

Related Questions