Unix-based system. I'm trying to use as little overhead as possible right now in the code I'm working on (it's in a resource constrained space). In this particular code, we are gathering some basic disk usage stats. One suggestion was to replace a call to df with statfs since df is a C utility that requires its own subprocess to run whereas statfs is a system call which presumably uses less overhead (and is what df calls anyway).
We're calling df with Python's subprocess.check_output() command:
import subprocess
DF_CMD = ["df", "-P", "-k"]
def get_disk_usage() -> str:
try:
output = subprocess.check_output(DF_CMD, text=True)
except subprocess.CalledProcessError as e:
raise RuntimeError(f"Failed to execute {DF_CMD} " + str(e)) from e
return output
I want to hard code our mount points (which we decided we're okay with) and replace the call to df with a call to statfs <mountpoint> in the above code. However, I'm unsure if calling with the same Python function will actually reduce overhead. I plan to use a profiler to check it, but I'm curious if anyone knows enough about the inner workings of Python/Unix to know what's going on under the hood?
And to be clear: by "overhead" I mean CPU and memory usage on the OS/machine.
df, or if you'll have to reconstruct the text output provided bydffrom the raw data provided byos.statvfs. You are still going to have to profile your actual replacement.)