In general, a subprocess doesn't share variables with its parent. It might share memory pages, though.
On POSIX systems Python launches a subprocess by using os.fork()
followed by os.execvp()
or os.execvpe()
. (On Windows it uses CreateProcess()
, but I'll focus on POSIX.)
After the fork
, the child process is a carbon copy of the parent. But unlike threads, they do not share memory (or variables) by design; they are separate processes. A forked process does have a copy of its parent process's descriptors, pointing to the same files.
Depending on the OS's implementation of virtual memory management they might share memory pages using copy on write ("COW").
However, shortly after that the child process calls os.execvp
to replace itself with another program.
Now this new program might still share some memory pages using COW depending on the aforementioned virtual memory implementation. But it cannot use variables from the process it replaces.
If you want the child process to be a Python function, you should look into multiprocessing
. There you can share variables between processes.