Last active
March 26, 2016 17:38
-
-
Save logston/806e4424c9fc86970ff3 to your computer and use it in GitHub Desktop.
Wait, really? Can you share state between processes?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nope. So why is the location in memory of
sys.modulesthe same between processes but state can not be shared between processes via thesys.modulesdict?