Skip to content

Instantly share code, notes, and snippets.

@brantfaircloth
Created September 12, 2014 15:55
Show Gist options
  • Save brantfaircloth/73bbad82a89ccfe98dd7 to your computer and use it in GitHub Desktop.
Save brantfaircloth/73bbad82a89ccfe98dd7 to your computer and use it in GitHub Desktop.
A numpy/multiprocessing surprise
import numpy as np
from multiprocessing import Pool
def test(top):
return np.random.random_integers(0,100,5)
def main():
# set this to a large CPU number
pool = Pool(4)
results = pool.map(test, [1,1,1,1,1,1])
print results
if __name__ == '__main__':
main()
from numpy.random import RandomState
from multiprocessing import Pool
def test(top):
prng = RandomState()
nums = prng.randint(0, 100, size=5)
return nums
def main():
pool = Pool(4)
results = pool.map(test, [1,1,1,1,1,1])
print results
if __name__ == '__main__':
main()
@brantfaircloth
Copy link
Author

Output from bad.py:

array([46, 38, 54, 65, 65]) 
array([46, 38, 54, 65, 65]) 
array([46, 38, 54, 65, 65]) 
array([ 1, 12, 75, 2, 22]) 
array([ 1, 12, 75, 2, 22]) 
array([44, 54, 65, 84, 86])] 

Output from good.py:

array([70, 84, 58, 97, 67]) 
array([12, 97, 89, 63, 90]) 
array([23, 72, 31, 72, 63]) 
array([38, 71, 29, 72, 9]) 
array([55, 32, 46, 42, 5]) 
array([50, 91, 91, 44, 81])] 

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