Skip to content
Advertisement

Is it possible to pass data other than string or list as argument for a subprocess in python?

I can start a sub-process with a list as arguments for example like this:

data = []
data.append("test")
subprocess.run(['python', 'myprocess.py'] + data)

My question is, if it is possible to pass bigger amounts of data to the sub-process? For example, if I have loaded a bunch of images and want to pass them directly as numpy arrays without having to store them to file and pass the paths as strings to load them again in the sub-process?

Advertisement

Answer

Argument vectors are arrays of C strings at the operating system level (see man execve). You can only pass data on the argv that can be represented as a C string (note that C strings are NUL-terminated, so no NULs allowed). You can certainly serialize data into a C string on one side and deserialize it on the other side, but that’s work.

Moreover, there’s a maximum amount of storage available that’s shared between environment variables and command-line arguments — so the more/larger environment variables you have, the shorter the combined length of all your command-line arguments can be.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement