This is similar to a previous question but for multiprocessing
instead of subprocess
. It seems that changing dynamically PYTHONHASHSEED
has no effect when using multiprocessing
, unlike subprocess
:
#check_environ.py
import os, multiprocessing, subprocess, sys
s = 'hello'
print('parent', os.getenv('PYTHONHASHSEED'), hash(s))
if len(sys.argv) > 1:
os.environ['PYTHONHASHSEED'] = sys.argv[1]
subprocess.call(['python', '-c', "import os;print('subprocess', os.getenv('PYTHONHASHSEED'), hash('{}'))".format(s)])
multiprocessing.Process(target=lambda:print('multiprocessing', os.getenv('PYTHONHASHSEED'), hash(s))).start()
Sample runs:
# explicit PYTHONHASHSEED for subprocess/multiprocessing
$ python check_environ.py 12
parent None 4472558296122225349
subprocess 12 -8207222429063474615
multiprocessing 12 4472558296122225349
# random PYTHONHASHSEED for subprocess/multiprocessing
$ python check_environ.py
parent None 7990499464460966677
subprocess None 1081030409066486350
multiprocessing None 7990499464460966677
So no matter what, the multiprocessing
hash uses the same seed as the parent. Is there a way to force subprocesses spawned by multiprocessing
use a different hash seed?