Random and fork
stephane at wirtel.be
Thu Feb 7 14:29:33 CET 2013
* Julien Le Goff <julien.legoff at gmail.com> [2013-02-06 08:28:24 -0800]:
> Hi everyone,
> Today I came accross a behaviour I did not expect in python (I am using 2.7). In my program, random.random() always seemed to return the same number; it turned out to be related to the fact that I was using os.fork.
> See below a small program that illustrates this. It is easily fixed, but I'm interested in knowing why this happens. Can anybody give me a hint? Thanks!
> import random
> import os
> for i in xrange(10):
> pid = os.fork()
> if pid == 0:
> # uncommenting this fixes the problem
> # random.seed(os.getpid())
> print random.random()
If you look at the code of gunicorn, you can see than there is a
random.seed() just after the fork syscall.
Try with that.
Stéphane Wirtel - http://wirtel.be - @matrixise
More information about the Python-list