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

cache: better locking #503

Open
wants to merge 2 commits into
base: main
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
30 changes: 5 additions & 25 deletions pyopencl/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,38 +77,18 @@ def error_clean_up(self):
class CacheLockManager(CleanupBase):
def __init__(self, cleanup_m, cache_dir):
if cache_dir is not None:
self.lock_file = os.path.join(cache_dir, "lock")
lock_file = os.path.join(cache_dir, "lock")

attempts = 0
while True:
try:
self.fd = os.open(self.lock_file,
os.O_CREAT | os.O_WRONLY | os.O_EXCL)
break
except OSError:
pass

from time import sleep
sleep(1)
from pyopencl.filelock import FileLock

attempts += 1
self.lock = FileLock(lock_file, timeout=30)

if attempts > 10:
from warnings import warn
warn("could not obtain cache lock--delete '%s' if necessary"
% self.lock_file)

if attempts > 3 * 60:
raise RuntimeError("waited more than three minutes "
"on the lock file '%s'"
"--something is wrong" % self.lock_file)
self.lock.acquire()

cleanup_m.register(self)

def clean_up(self):
import os
os.close(self.fd)
os.unlink(self.lock_file)
self.lock.release()

def error_clean_up(self):
pass
Expand Down
Loading