Skip to content

Instantly share code, notes, and snippets.

@logston
Last active March 26, 2016 17:38
Show Gist options
  • Select an option

  • Save logston/806e4424c9fc86970ff3 to your computer and use it in GitHub Desktop.

Select an option

Save logston/806e4424c9fc86970ff3 to your computer and use it in GitHub Desktop.
Wait, really? Can you share state between processes?
"""
Can you share state between processes my updating sys.modules?
"""
import multiprocessing
import sys
import time
def change_state():
print('Before change', id(sys.modules))
sys.modules['test'] = 'moose'
print('After change', id(sys.modules))
def check_state():
time.sleep(1)
print('Check', id(sys.modules))
assert sys.modules['test'] == 'moose'
def main():
p1 = multiprocessing.Process(target=change_state)
p2 = multiprocessing.Process(target=check_state)
p1.start()
p2.start()
p1.join()
p2.join()
main()
Before change 4374219592
After change 4374219592
Check 4374219592
Process Process-2:
Traceback (most recent call last):
File "/Users/paul/.pyenv/versions/3.5.0/lib/python3.5/multiprocessing/process.py", line 254, in _bootstrap
self.run()
File "/Users/paul/.pyenv/versions/3.5.0/lib/python3.5/multiprocessing/process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "multi2.py", line 18, in check_state
assert sys.modules['test'] == 'moose'
KeyError: 'test'
@logston
Copy link
Copy Markdown
Author

logston commented Mar 26, 2016

Nope. So why is the location in memory of sys.modules the same between processes but state can not be shared between processes via the sys.modules dict?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment