From 30c5af298ac33aff3d0aeb72010bb7163513bb2c Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 5 Mar 2024 21:51:22 +0900 Subject: [PATCH 1/7] gh-174: Support free-threading CPython by disabling psutil related features --- doc/changelog.rst | 8 ++++++++ pyperf/_collect_metadata.py | 6 +++++- pyperf/_cpu_utils.py | 17 +++++++++++++---- pyperf/_psutil_memory.py | 6 +++++- pyperf/_utils.py | 3 ++- 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/doc/changelog.rst b/doc/changelog.rst index 378de1d6..6d25db03 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -1,6 +1,14 @@ Changelog ========= +Version 2.6.3 (2024-03-05) +--------------------------- + +* Support Free-threading CPython (PEP-703) by disabling psutil related features. + Patch by Donghee Na. +* Fix mem_max_rss measurement on macOS. + Patch by Mike Droettboom. + Version 2.6.2 (2023-11-02) --------------------------- diff --git a/pyperf/_collect_metadata.py b/pyperf/_collect_metadata.py index e504ab6d..8ba941fa 100644 --- a/pyperf/_collect_metadata.py +++ b/pyperf/_collect_metadata.py @@ -11,8 +11,12 @@ resource = None try: + from pyperf._utils import IS_FREE_THREADING # Optional dependency - import psutil + if IS_FREE_THREADING: + psutil = None + else: + import psutil except ImportError: psutil = None diff --git a/pyperf/_cpu_utils.py b/pyperf/_cpu_utils.py index ea230109..6bdf900c 100644 --- a/pyperf/_cpu_utils.py +++ b/pyperf/_cpu_utils.py @@ -2,11 +2,14 @@ import os import re -from pyperf._utils import sysfs_path, proc_path, read_first_line +from pyperf._utils import sysfs_path, proc_path, read_first_line, IS_FREE_THREADING try: # Optional dependency - import psutil + if IS_FREE_THREADING: + psutil = None + else: + import psutil except ImportError: psutil = None @@ -152,7 +155,10 @@ def set_cpu_affinity(cpus): return True try: - import psutil + if IS_FREE_THREADING: + return + else: + import psutil except ImportError: return @@ -166,7 +172,10 @@ def set_cpu_affinity(cpus): def set_highest_priority(): try: - import psutil + if IS_FREE_THREADING: + return + else: + import psutil except ImportError: return diff --git a/pyperf/_psutil_memory.py b/pyperf/_psutil_memory.py index f56cfb4a..24ec459e 100644 --- a/pyperf/_psutil_memory.py +++ b/pyperf/_psutil_memory.py @@ -1,6 +1,10 @@ import os try: - import psutil + from pyperf._utils import IS_FREE_THREADING + if IS_FREE_THREADING: + raise ImportError + else: + import psutil except ImportError: raise ImportError('psutil is not installed') import threading diff --git a/pyperf/_utils.py b/pyperf/_utils.py index a5ff155f..73b3c861 100644 --- a/pyperf/_utils.py +++ b/pyperf/_utils.py @@ -3,10 +3,11 @@ import os import statistics import sys +import sysconfig from shlex import quote as shell_quote # noqa from shutil import which - +IS_FREE_THREADING = bool(sysconfig.get_config_var('Py_GIL_DISABLED')) MS_WINDOWS = (sys.platform == 'win32') MAC_OS = (sys.platform == 'darwin') From 08eba5b3eee791e85a38d21278f0ebf746f17d08 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 5 Mar 2024 22:07:21 +0900 Subject: [PATCH 2/7] Address code review --- pyperf/_collect_metadata.py | 6 ++++-- pyperf/_cpu_utils.py | 14 ++++++++++---- pyperf/_psutil_memory.py | 4 +++- pyperf/_utils.py | 2 +- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/pyperf/_collect_metadata.py b/pyperf/_collect_metadata.py index 8ba941fa..7b8b39f4 100644 --- a/pyperf/_collect_metadata.py +++ b/pyperf/_collect_metadata.py @@ -11,9 +11,11 @@ resource = None try: - from pyperf._utils import IS_FREE_THREADING + from pyperf._utils import FREE_THREADING # Optional dependency - if IS_FREE_THREADING: + # Currently there is a packaging issue for PEP-703, + # Until then psutil is disabled as a workaround. + if FREE_THREADING: psutil = None else: import psutil diff --git a/pyperf/_cpu_utils.py b/pyperf/_cpu_utils.py index 6bdf900c..61b94297 100644 --- a/pyperf/_cpu_utils.py +++ b/pyperf/_cpu_utils.py @@ -2,11 +2,13 @@ import os import re -from pyperf._utils import sysfs_path, proc_path, read_first_line, IS_FREE_THREADING +from pyperf._utils import sysfs_path, proc_path, read_first_line, FREE_THREADING try: # Optional dependency - if IS_FREE_THREADING: + # Currently there is a packaging issue for PEP-703, + # Until then psutil is disabled as a workaround. + if FREE_THREADING: psutil = None else: import psutil @@ -155,7 +157,9 @@ def set_cpu_affinity(cpus): return True try: - if IS_FREE_THREADING: + # Currently there is a packaging issue for PEP-703, + # Until then psutil is disabled as a workaround. + if FREE_THREADING: return else: import psutil @@ -172,7 +176,9 @@ def set_cpu_affinity(cpus): def set_highest_priority(): try: - if IS_FREE_THREADING: + # Currently there is a packaging issue for PEP-703, + # Until then psutil is disabled as a workaround. + if FREE_THREADING: return else: import psutil diff --git a/pyperf/_psutil_memory.py b/pyperf/_psutil_memory.py index 24ec459e..3190d3f0 100644 --- a/pyperf/_psutil_memory.py +++ b/pyperf/_psutil_memory.py @@ -1,6 +1,8 @@ import os try: - from pyperf._utils import IS_FREE_THREADING + from pyperf._utils import FREE_THREADING + # Currently there is a packaging issue for PEP-703, + # Until then psutil is disabled as a workaround. if IS_FREE_THREADING: raise ImportError else: diff --git a/pyperf/_utils.py b/pyperf/_utils.py index 73b3c861..a894dd9a 100644 --- a/pyperf/_utils.py +++ b/pyperf/_utils.py @@ -7,7 +7,7 @@ from shlex import quote as shell_quote # noqa from shutil import which -IS_FREE_THREADING = bool(sysconfig.get_config_var('Py_GIL_DISABLED')) +FREE_THREADING = bool(sysconfig.get_config_var('Py_GIL_DISABLED')) MS_WINDOWS = (sys.platform == 'win32') MAC_OS = (sys.platform == 'darwin') From 76f8e27dfbb4d9e60b21d0e3024bfff2772a73ea Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Tue, 5 Mar 2024 22:09:10 +0900 Subject: [PATCH 3/7] fix --- pyperf/_psutil_memory.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyperf/_psutil_memory.py b/pyperf/_psutil_memory.py index 3190d3f0..2afda531 100644 --- a/pyperf/_psutil_memory.py +++ b/pyperf/_psutil_memory.py @@ -3,7 +3,7 @@ from pyperf._utils import FREE_THREADING # Currently there is a packaging issue for PEP-703, # Until then psutil is disabled as a workaround. - if IS_FREE_THREADING: + if FREE_THREADING: raise ImportError else: import psutil From 8cadb487422f0623d6b6167b4279f517dc1b9ab7 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 6 Mar 2024 00:39:19 +0900 Subject: [PATCH 4/7] Update --- doc/changelog.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/changelog.rst b/doc/changelog.rst index 6d25db03..aa18ebea 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -5,6 +5,7 @@ Version 2.6.3 (2024-03-05) --------------------------- * Support Free-threading CPython (PEP-703) by disabling psutil related features. + Relavant issue: https://github.com/python/cpython/issues/116024 Patch by Donghee Na. * Fix mem_max_rss measurement on macOS. Patch by Mike Droettboom. From e245162f45fda9a36e645f5a7c7fddc476ad724b Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 6 Mar 2024 05:17:04 +0900 Subject: [PATCH 5/7] Update doc/changelog.rst Co-authored-by: Victor Stinner --- doc/changelog.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changelog.rst b/doc/changelog.rst index aa18ebea..6b57351a 100644 --- a/doc/changelog.rst +++ b/doc/changelog.rst @@ -5,7 +5,7 @@ Version 2.6.3 (2024-03-05) --------------------------- * Support Free-threading CPython (PEP-703) by disabling psutil related features. - Relavant issue: https://github.com/python/cpython/issues/116024 + Relevant issue: https://github.com/python/cpython/issues/116024. Patch by Donghee Na. * Fix mem_max_rss measurement on macOS. Patch by Mike Droettboom. From f8667dc35ae06494e6aa1983528d60d76e3b29ea Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 6 Mar 2024 18:44:48 +0900 Subject: [PATCH 6/7] Address code review --- pyperf/_collect_metadata.py | 7 ++----- pyperf/_cpu_utils.py | 11 +++-------- pyperf/_psutil_memory.py | 6 ++---- pyperf/_utils.py | 5 ++++- 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/pyperf/_collect_metadata.py b/pyperf/_collect_metadata.py index 7b8b39f4..e69bc8ea 100644 --- a/pyperf/_collect_metadata.py +++ b/pyperf/_collect_metadata.py @@ -11,11 +11,8 @@ resource = None try: - from pyperf._utils import FREE_THREADING - # Optional dependency - # Currently there is a packaging issue for PEP-703, - # Until then psutil is disabled as a workaround. - if FREE_THREADING: + from pyperf._utils import USE_PSUTIL + if not USE_PSUTIL: psutil = None else: import psutil diff --git a/pyperf/_cpu_utils.py b/pyperf/_cpu_utils.py index 61b94297..0a995686 100644 --- a/pyperf/_cpu_utils.py +++ b/pyperf/_cpu_utils.py @@ -2,13 +2,10 @@ import os import re -from pyperf._utils import sysfs_path, proc_path, read_first_line, FREE_THREADING +from pyperf._utils import sysfs_path, proc_path, read_first_line, USE_PSUTIL try: - # Optional dependency - # Currently there is a packaging issue for PEP-703, - # Until then psutil is disabled as a workaround. - if FREE_THREADING: + if not USE_PSUTIL: psutil = None else: import psutil @@ -157,9 +154,7 @@ def set_cpu_affinity(cpus): return True try: - # Currently there is a packaging issue for PEP-703, - # Until then psutil is disabled as a workaround. - if FREE_THREADING: + if not USE_PSUTIL: return else: import psutil diff --git a/pyperf/_psutil_memory.py b/pyperf/_psutil_memory.py index 2afda531..ca6776ed 100644 --- a/pyperf/_psutil_memory.py +++ b/pyperf/_psutil_memory.py @@ -1,9 +1,7 @@ import os try: - from pyperf._utils import FREE_THREADING - # Currently there is a packaging issue for PEP-703, - # Until then psutil is disabled as a workaround. - if FREE_THREADING: + from pyperf._utils import USE_PSUTIL + if not USE_PSUTIL: raise ImportError else: import psutil diff --git a/pyperf/_utils.py b/pyperf/_utils.py index a894dd9a..87bb1a7f 100644 --- a/pyperf/_utils.py +++ b/pyperf/_utils.py @@ -7,7 +7,10 @@ from shlex import quote as shell_quote # noqa from shutil import which -FREE_THREADING = bool(sysconfig.get_config_var('Py_GIL_DISABLED')) +# Currently there is a packaging issue for PEP-703, +# Until then psutil is disabled as a workaround. +# See: https://github.com/python/cpython/issues/116024 +USE_PSUTIL = not bool(sysconfig.get_config_var('Py_GIL_DISABLED')) MS_WINDOWS = (sys.platform == 'win32') MAC_OS = (sys.platform == 'darwin') From 603a1a51dbe0781f99ea658be9425bec22824f6e Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 6 Mar 2024 18:47:29 +0900 Subject: [PATCH 7/7] fix --- pyperf/_cpu_utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/pyperf/_cpu_utils.py b/pyperf/_cpu_utils.py index 0a995686..f810df25 100644 --- a/pyperf/_cpu_utils.py +++ b/pyperf/_cpu_utils.py @@ -171,9 +171,7 @@ def set_cpu_affinity(cpus): def set_highest_priority(): try: - # Currently there is a packaging issue for PEP-703, - # Until then psutil is disabled as a workaround. - if FREE_THREADING: + if not USE_PSUTIL: return else: import psutil