Last active
August 28, 2024 22:07
-
-
Save moreati/b8d157ff82cb15234bece4033accc5e5 to your computer and use it in GitHub Desktop.
Demonstration of Python subprocess.Popen() ResourceWarning
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
import sys | |
import warnings | |
# By default ResourceWarning output (and other) is hidden. | |
# Setting environment variable PYTHONWARNINGS=default will | |
# show them, similar to the below. | |
if not sys.warnoptions: | |
warnings.simplefilter('default') | |
proc1 = subprocess.Popen(['true']) | |
print(f'{proc1.pid=}') | |
print(f'{os.waitpid(proc1.pid, 0)=}') | |
# This will emit a ResourceWarning (on CPython atleast) | |
del proc1 | |
print() | |
proc2 = subprocess.Popen(['true']) | |
print(f'{proc2.pid=}') | |
print(f'{proc2.wait()=}') | |
# This will not emit a ResourceWarning | |
del proc2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Although the ResourceWarning says "subprocess 27554 is still running", this is not strictly true. By calling
os.waitpid()
directly we have allowed the operating systems to clean up any zombie child process, but because we've bypassed theproc1
Popen object it is unaware.Instead of using
os.waitpid()
or other variants we should use thePopen.wait()
method, as shown withproc2
.The ResourceWarning text could possibly be worded more clearly.