[Numpy-discussion] numpy.random.RandomState threadsafe?

Robert Kern robert.kern@gmail....
Tue Mar 11 17:00:57 CDT 2008

On Tue, Mar 11, 2008 at 4:18 PM, Hoyt Koepke <hoytak@gmail.com> wrote:
> This should be a really quick question.  Is a RandomState object
>  thread safe?  I'm wanting to use a common RandomState object in a
>  multithreaded program, and I need to know if it's necessary to protect
>  it with a lock (which wouldn't be difficult).

For nearly all of the methods, yes, they should be. RandomState is
implemented in C (using Pyrex) and the GIL is acquired before calling
any C functions. The caveat here is that the methods
multivariate_normal() calls back out to Python-implemented functions.
It is possible that the GIL gets released during that call and that
another thread can pick up execution then. However, even this should
not be a problem as far as safety goes; no internal state is read or
changed after the external call.

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco

More information about the Numpy-discussion mailing list