Last active
October 25, 2017 14:57
-
-
Save suchow/3cd1fa50234a1d5cf31fa2f242487039 to your computer and use it in GitHub Desktop.
Reproducible multiprocessing map
This file contains 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
"""Reproducible multiprocessing. | |
Output: [0.6320908733609572, 0.6407083081891108, 0.19120526615722777, ... | |
""" | |
import multiprocessing as mp | |
import random | |
random.seed(1) | |
def unpack_seed_apply(fargseed): | |
"""Unpack, seed the PRNG, and apply the function.""" | |
(f, args, seed) = fargseed | |
random.seed(seed) | |
return f(args) | |
def mapr(f, args): | |
"""Reproducible map with a multiprocessing pool.""" | |
fs = [f for _ in args] | |
seeds = [random.getrandbits(128) for _ in range(len(args))] | |
fargseeds = zip(fs, args, seeds) | |
pool = mp.Pool(20) | |
return pool.map(unpack_seed_apply, fargseeds) | |
def f(x): | |
"""Demo function.""" | |
return random.random() | |
results = mapr(f, range(10)) | |
print(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment