In Linux, I understand whenever you fork a child process, the child process gets a copy of the parent process address space.
I don't understand why this is always desirable. The parent process may have global objects that should not be shared between parent and child process. Why do we want child process to always get them?
For example, let's say parent process maintains a global database client.
client.py
client = None
def get_client():
global client
if client is None:
client = make_new_db_client()
return client
This works fine in my single process application. But could break once I start using multiprocessing.
The problem is that I'm sharing db_client object across processes. This database code is actually a 3rd party library. And let's pretend for a second it simply does not work across multiprocesses. So, I create a db_client in the parent process, and I'm forcing the child process to use it. It'd fail.
I think the right thing to do is to allow user to selectively pick what gets shared across multiprocess. So in this case, I simply won't share the db_client. Child process would start up and create its own database connection. Everything would work.
But that's not the way Python is designed. global variables are always copied (at least in Linux). Why is it desirable to do that? That seems like it's good sometimes but not always.
Or is there already a way to selectively hide variables from child process?