import logging import multiprocessing import multiprocessing.sharedctypes """Test showing that multiprocessing.Value instances are not synchronized. Following tests performed on 2013-01-19. Note that on a single CPU machine the error is not reported for every execution but only for about every third run. $ python --version Python 2.6.5 $ uname -a Linux ubuntu-desktop 2.6.32.11+drm33.2 #2 Fri Jun 18 20:30:49 CEST 2010 i686 GNU/Linux $ python test_multiprocessing.py ERROR:root:Unexpected value: 99 (expected: 100) On a quadcore, the error occurs almost every time. $ uname -a Linux PC 2.6.35.13 #4 SMP Tue Dec 20 15:22:02 CET 2011 x86_64 GNU/Linux ~/local/Python-2.7.3/python test_multiprocessing.py ERROR:root:Unexpected value: 95 (expected: 100) ~/local/Python-3.3.0/python test_multiprocessing.py ERROR:root:Unexpected value: 86 (expected: 100) """ def do_inc(integer): """Increment integer.value for multiprocessing.Value integer.""" integer.value += 1 def do_test(notasks): """Create notasks processes, each incrementing the same Value. As the Value is initialized to 0, its final value is expected to be notasks. """ tasks = list() integer = multiprocessing.sharedctypes.Value("i", 0) for run in range(notasks): proc = multiprocessing.Process(target=do_inc, args=(integer,)) proc.start() tasks.append(proc) for proc in tasks: proc.join() if integer.value != notasks: logging.error( "Unexpected value: %d (expected: %d)", integer.value, notasks) if __name__ == "__main__": do_test(100)