From fe6a8cc4bc034955d654611ac9e5147e7fcd7038 Mon Sep 17 00:00:00 2001 From: Prabhu Ramachandran Date: Thu, 26 Sep 2024 18:40:33 +0530 Subject: [PATCH] Use fasteners' inter process lock ... ... instead of our own. This should sort out issues on Windows where tests fail often due to issues with locking. --- compyle/ext_module.py | 30 ++++++------------------------ pyproject.toml | 1 + requirements.txt | 1 + setup.py | 2 +- 4 files changed, 9 insertions(+), 25 deletions(-) diff --git a/compyle/ext_module.py b/compyle/ext_module.py index 6ecc223..e6d13f4 100644 --- a/compyle/ext_module.py +++ b/compyle/ext_module.py @@ -3,6 +3,7 @@ from distutils.sysconfig import get_config_vars from distutils.util import get_platform from distutils.errors import CompileError, LinkError +from fasteners import InterProcessLock import hashlib import importlib import io @@ -14,7 +15,6 @@ from pyximport import pyxbuild import shutil import sys -import time # Conditional/Optional imports. if sys.platform == 'win32': @@ -143,6 +143,7 @@ def __init__(self, src, extension='pyx', root=None, verbose=False, extra_compile_args if extra_compile_args else [] ) self.extra_link_args = extra_link_args if extra_link_args else [] + self.lck = InterProcessLock(self.lock_path) def _add_local_include(self): if 'bsd' in platform.system().lower(): @@ -158,32 +159,13 @@ def _setup_filenames(self): @contextmanager def _lock(self, timeout=90): - t1 = time.time() - - def _is_timed_out(): - if timeout is None: - return False - else: - return (time.time() - t1) > timeout - - def _try_to_lock(): - if not exists(self.lock_path): - try: - os.mkdir(self.lock_path) - except OSError: - return False - else: - return True - return False - - while not _try_to_lock(): - time.sleep(0.1) - if _is_timed_out(): - break + self.lck.acquire(timeout) try: yield finally: - os.rmdir(self.lock_path) + self.lck.release() + if exists(self.lock_path): + os.remove(self.lock_path) def _write_source(self, path): if not exists(path): diff --git a/pyproject.toml b/pyproject.toml index 3fe2570..137c749 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,5 +5,6 @@ requires = [ "numpy>=2.0,<3", "Cython>=0.20", "mako", + "fasteners", "pytools" ] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index db7fea3..a2ec8ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ mako pytools cython numpy +fasteners pytest diff --git a/setup.py b/setup.py index 97e48f1..d28edd8 100644 --- a/setup.py +++ b/setup.py @@ -19,7 +19,7 @@ def get_version(): return data.get('__version__') -install_requires = ['mako', 'pytools', 'cython', 'numpy'] +install_requires = ['mako', 'pytools', 'cython', 'numpy', 'fasteners'] tests_require = ['pytest'] if sys.version_info[0] < 3: tests_require += ['mock>=1.0']