Skip to content
Open
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
9 changes: 7 additions & 2 deletions ilock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ def __enter__(self):

current_time = call_time = time()
while call_time + self._timeout >= current_time:
self._lockfile = open(self._filepath, 'w')
try:
self._lockfile = open(self._filepath, 'w')
portalocker.lock(self._lockfile, portalocker.constants.LOCK_NB | portalocker.constants.LOCK_EX)
self._enter_count = 1
return self
except portalocker.exceptions.LockException:
pass
except PermissionError:
pass

current_time = time()
check_interval = self._check_interval if self._timeout > self._check_interval else self._timeout
Expand All @@ -56,7 +58,10 @@ def __exit__(self, exc_type, exc_val, exc_tb):

if sys.platform.startswith('linux'):
# In Linux you can delete a locked file
os.unlink(self._filepath)
try:
os.unlink(self._filepath)
except FileNotFoundError:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good change that I want to add too.
In rare cases that happen from time to time, its happen.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I explained why this wouldn't work in #7 (comment).

pass

self._lockfile.close()

Expand Down