In one C++ source file that I’m wrapping in a python function, someone has included the following:
namespace some_namespace
{
static double some_double;
}
float function_that_uses_some_double(float input) {
// implementation
return result;
}
The static global some_double is only ever used inside the function, so if I wrap this in a CPython function and call it in single-threaded code, the variable will only ever be used by one function at a time. It’s ugly, but no problem there. My question is what happens if I use:
- the
threadingmodule, or - the
multiprocessingmodule.
When I have multiple processes and / or threads using this module, will they interfere with each other?
If you use the threading module, then all functions will simply share that global variable. Threads in python are switched between bytecode boundaries, so locking is a non-issue.
If you use the multiprocessing module, things are different and it depends a bit on your usage of multiprocessing. Python starts with a single process so, there is only one copy of the global variable. The value of that variable when you start using multiprocessing (i.e. forking new python processes from the main process) will be copied into the subprocesses (tasks), but those processes will each have their own copy of the global variable.
If you get tricky and set up a shared memory segment (mmap with MAP_SHARED) and the variable is a pointer, then the location pointed to will be shared and you’ll need to use locking.