Ryuichi
Ryuichi

Reputation: 3

Suppress file output in a python subprocess

I have a binary executable that when run writes ~10 or so files and also writes to stdout. For some reason the program does not provide the option to not write any of these files.

I'd like to run this program as a python subprocess, but to reduce I/O operations I would like to prevent the files from being written to disk altogether and collect only the stdout. Is this possible?

I've tried running subprocess.run with capture_output=True and a few other flags but haven't had any luck.

Thanks in advance for the help!

Upvotes: 0

Views: 82

Answers (1)

Balaïtous
Balaïtous

Reputation: 896

If you have enough memory you can use /dev/shm.

$ df -h
tmpfs               16G    4,0K   16G   1% /dev/shm

Assuming your binary write files to current working directory:

with tempfile.TemporaryDirectory(dir="/dev/shm") as workdir:
    p = subprocess.run(("binary",), capture_output=True, cwd=workdir)

If memory is a problem, you can override open function using LD_PRELOAD and redirect writes to /dev/null.

Upvotes: 0

Related Questions