Skip to content

Commit

Permalink
Use fasteners' inter process lock ...
Browse files Browse the repository at this point in the history
... instead of our own. This should sort out issues on Windows where
tests fail often due to issues with locking.
  • Loading branch information
prabhuramachandran committed Sep 26, 2024
1 parent dd566c3 commit fe6a8cc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 25 deletions.
30 changes: 6 additions & 24 deletions compyle/ext_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,7 +15,6 @@
from pyximport import pyxbuild
import shutil
import sys
import time

# Conditional/Optional imports.
if sys.platform == 'win32':
Expand Down Expand Up @@ -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():
Expand All @@ -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):
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ requires = [
"numpy>=2.0,<3",
"Cython>=0.20",
"mako",
"fasteners",
"pytools"
]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ mako
pytools
cython
numpy
fasteners
pytest
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down

0 comments on commit fe6a8cc

Please sign in to comment.