From 2a73912c9cbb579207ff6ca4eeff78c2fddd3d69 Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Thu, 14 Dec 2023 04:28:27 +0000 Subject: [PATCH 1/3] Speed up import of threading module Delayed import of functools leads to 50% speedup of import time. --- Lib/threading.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/threading.py b/Lib/threading.py index ecf799bc26ab06..136c29572874a9 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -3,7 +3,6 @@ import os as _os import sys as _sys import _thread -import functools import warnings import _weakref @@ -1630,6 +1629,7 @@ def _register_atexit(func, *arg, **kwargs): if _SHUTTING_DOWN: raise RuntimeError("can't register atexit after shutdown") + import functools call = functools.partial(func, *arg, **kwargs) _threading_atexits.append(call) From b70b7e58cadf34b133292014c8cf84dd84f49d9f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 23 Jan 2024 23:13:48 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2024-01-23-23-13-47.gh-issue-109653.KLBHmT.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2024-01-23-23-13-47.gh-issue-109653.KLBHmT.rst diff --git a/Misc/NEWS.d/next/Library/2024-01-23-23-13-47.gh-issue-109653.KLBHmT.rst b/Misc/NEWS.d/next/Library/2024-01-23-23-13-47.gh-issue-109653.KLBHmT.rst new file mode 100644 index 00000000000000..76074df9c76fa6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-01-23-23-13-47.gh-issue-109653.KLBHmT.rst @@ -0,0 +1 @@ +Reduce the import time of :mod:`threading` module by ~50%. Patch by Daniel Hollas. From e76cec2d321817c0914b7693dc7c1fac98bdc67b Mon Sep 17 00:00:00 2001 From: Daniel Hollas Date: Wed, 24 Jan 2024 13:51:28 +0000 Subject: [PATCH 3/3] Let's got with lambda instead of functools.partial Co-authored-by: Alex Waygood --- Lib/threading.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Lib/threading.py b/Lib/threading.py index 136c29572874a9..5bc8fd33661c86 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -1629,9 +1629,7 @@ def _register_atexit(func, *arg, **kwargs): if _SHUTTING_DOWN: raise RuntimeError("can't register atexit after shutdown") - import functools - call = functools.partial(func, *arg, **kwargs) - _threading_atexits.append(call) + _threading_atexits.append(lambda: func(*arg, **kwargs)) from _thread import stack_size