Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fasteners' inter process lock ... #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 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,11 @@ 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()

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
Loading