New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document that the random module doesn't support fork #74237
Comments
When reviewing the issue bpo-30030, it reminded me that the random module "doesn't support fork": after fork, the parent and the child produce the same "random" number sequence. I suggest to add a quick note about that: not a warning, just a note, as a reminder. There is an exception: SystemRandom produces a different sequence after fork, since it uses os.urandom() (which runs in the kernel). I am tempted to propose a solution in the note like using SystemRandom, but I'm not sure that it's a good idea. Some users may misunderstood the note and always use SystemRandom where random.Random is just fine for their needs. Proposed note: The random module doesn't support fork: the parent and the child process will produce the same number sequence. Proposed solution: If your code uses os.fork(), a workaround is to check if os.getpid() changes and in that case, create a new Random instance or reseed the RNG in the child process. -- The tempfile module reminds the pid and instanciates a new RNG on fork. Another option is to not add a note, but implement the workaround directly into the random module. But I don't think that it's worth it. Forking is a rare usecase, and calling os.getpid() may slowdown the random module. (I don't recall if os.getpid() requires a syscall or not, I'm quite sure that it's optimized at least on Linux to avoid a real syscall.) |
I'm not sure that's necessary. fork() is a low-level primitive, people can/should use multiprocessing.Process instead, which does re-seed the PRNG. |
Do you mean that adding a note is not necessary? Or proposing a |
I mean mentioning fork(), which people usually don't call directly, is not necessary, so, indeed, a note isn't necessary. |
I think it is worth documenting. Even if multiprocessing reseeds the module-global random generator, it doesn't reseed other instances of Random. |
Several thoughts:
|
os.fork() documentation contains a warning that refers to ssl documentation where the use of OpenSSL’s internal random number generator in forked processes is documented more detailed. I think the same should be added for the random module. |
+1 It is reasonable to extend the docs for os.fork(). |
Victor, this issue should be obsolete now that the global Random instance is reseeded after fork(). |
Even if the global Random instance is reseeded after fork(), other instances are not reseeded. |
At least, it would be nice to document it for Python 3.6 and older. |
This is true, but it's by design. If you want a specific random sequence (which is the primary use case for specific instances), you don't want fork() to mess with the random sequence. |
Yes, this is a feature, not a bug. But I think it is worth be explicitly documented. |
Since Raymond and Antoine don't like the documentation idea, I just close my issue. Hopefully, Python 3.7 now reseeds automatically random at fork! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: