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

Consider proving a flock based lock #53

Open
psarka opened this issue Nov 8, 2020 · 0 comments
Open

Consider proving a flock based lock #53

psarka opened this issue Nov 8, 2020 · 0 comments

Comments

@psarka
Copy link
Collaborator

psarka commented Nov 8, 2020

@redramen writes:

I tried using the default fcntl lock with a daemon process (one generated by python-daemon package) but was running into a few issues. Notably I could not write my PID to a file, nor could I read it back with that style of locking. I can do those with the BSD style flock.

And provides the following snippet:

class _FlockLock(_InterProcessLock):
    """Interprocess lock implementation that works on posix systems based
    on https://www.man7.org/linux/man-pages/man2/flock.2.html."""

    @staticmethod
    def _trylock(lockfile):
        fileno = lockfile.fileno()
        fcntl.flock(fileno, fcntl.LOCK_EX | fcntl.LOCK_NB)

    @staticmethod
    def _unlock(lockfile):
        fileno = lockfile.fileno()
        fcntl.flock(fileno, fcntl.LOCK_UN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant