Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
csppfetch
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cspp_geo
csppfetch
Commits
17ecd0c8
Commit
17ecd0c8
authored
Jul 23, 2021
by
Alan De Smet
Browse files
Options
Downloads
Patches
Plain Diff
Expand docs; add simple doctest
parent
95348658
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
csppfetch/exclusivelockfile.py
+21
-14
21 additions, 14 deletions
csppfetch/exclusivelockfile.py
with
21 additions
and
14 deletions
csppfetch/exclusivelockfile.py
+
21
−
14
View file @
17ecd0c8
...
...
@@ -5,24 +5,31 @@ import logging
class
ExclusiveLockFile
:
"""
Lock a temporary file created for the purpose.
Use with
"
with
"
:
Intended to be used in a
"
with
"
block as a context manager.
with ExclusiveLockFile(
"
/tmp/my-lock
"
):
do_stuff()
The file will be created if it doesn
'
t exist. It will be deleted when the
block exits! The filename should not be a directory.
The file will be created if it doesn
'
t exist. It will be
deleted when the block exits!
If multiple processes try to lock the same file, the first
to succeed will delete it when done. That means that a process
starting after that point will create _new_ file and lock that,
not sharing the previous one! This makes it only suitable for
VERY specialized purposes (like ensuring only one process
downloads a file, and later processes will immediately bail out
If multiple processes try to lock the same file, the first to succeed will
delete it when done. That means that a process starting after that point
will create _new_ file and lock that, not sharing the previous one! This
makes it only suitable for VERY specialized purposes (like ensuring only
one process downloads a file, and later processes will immediately bail out
if the downloaded file is already preent).
Be warned that this is implemented using fcntl.lockf which is an ADVISORY
lock; only code using fcntl.lockf or similar will resepect it.
WILL create missing directories if necessary, will NOT
remove them on completion.
>>>
import
tempfile
>>>
with
tempfile
.
TemporaryDirectory
()
as
dir
:
...
with
ExclusiveLockFile
(
dir
+
"
/lock
"
):
...
# In this block, the file f.name is exclusively locked
...
# using fcntl.lockf. (Only code using fcntl.lockf or
...
# equivalent will respect this!)
...
pass
"""
def
__init__
(
self
,
filename
):
self
.
filename
=
filename
...
...
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment