If you try running this code:
from multiprocessing import Pool
from multiprocessing.pool import ThreadPool
import random
def do_thing(upper):
print(random.randint(0,99))
random.seed(0)
with ThreadPool(1) as pool:
list(pool.imap(do_thing, [99]))
with ThreadPool(1) as pool:
list(pool.imap(do_thing, [99]))
with Pool(1) as pool:
list(pool.imap(do_thing, [99]))
with Pool(1) as pool:
list(pool.imap(do_thing, [99]))
You will find that the ThreadPool
s print consistent integers across multiple runs, but the Pool
s don't. I get from here that we can't guarantee in which order the processes will be created so in many cases it would be impossible to guarantee consistent results. But in my case, there are only so many orders this could happen in, but there are many different outcomes. So I don' think that the linked post is explaining what's happening here.
Note that I want to "propagate" the seed, not reseed with the same number. I don't want the outputs to be all the same.
Also, it looks like this might be possible with a manager, but just wondering if there's an easier "obvious" way that I don't know about.
Aucun commentaire:
Enregistrer un commentaire