From 134a82c03698ea3951e72c28f3ddd1c30bbee470 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 30 Apr 2024 00:41:27 +0900 Subject: [PATCH 1/3] gh-118392: Add warning about random.random for multi thread app --- Doc/library/random.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 8fbce18c56f17c..e89cc83b7ed5df 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -294,6 +294,10 @@ be found in any statistics text. Return the next random floating point number in the range ``0.0 <= X < 1.0`` + .. warning:: + Directly calling this function in a multithreaded application can cause the + performance degradation. Instead, use the :class:`Random` per thread. + .. function:: uniform(a, b) From 53dc69d1d84e8ada33150b55773297806d3457e3 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 30 Apr 2024 01:08:09 +0900 Subject: [PATCH 2/3] revise --- Doc/library/random.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index e89cc83b7ed5df..401a66bdc3cd23 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -295,8 +295,8 @@ be found in any statistics text. Return the next random floating point number in the range ``0.0 <= X < 1.0`` .. warning:: - Directly calling this function in a multithreaded application can cause the - performance degradation. Instead, use the :class:`Random` per thread. + Calling this function in the concurrent use can cause performance degradation. + Instead, Consider using :class:`Random` per thread. .. function:: uniform(a, b) From 772abb5faa42275772b7549b1d907d69e520b416 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 30 Apr 2024 08:48:26 +0900 Subject: [PATCH 3/3] Address code reivew --- Doc/library/random.rst | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index 401a66bdc3cd23..61798263d61195 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -59,6 +59,12 @@ from sources provided by the operating system. random number generator with a long period and comparatively simple update operations. +.. note:: + The global random number generator and instances of :class:`Random` are thread-safe. + However, in the free-threaded build, concurrent calls to the global generator or + to the same instance of :class:`Random` may encounter contention and poor performance. + Consider using separate instances of :class:`Random` per thread instead. + Bookkeeping functions --------------------- @@ -294,10 +300,6 @@ be found in any statistics text. Return the next random floating point number in the range ``0.0 <= X < 1.0`` - .. warning:: - Calling this function in the concurrent use can cause performance degradation. - Instead, Consider using :class:`Random` per thread. - .. function:: uniform(a, b)