-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change add proper file locking to gunicorn. By default "gunicorn.lock" is created in the temporary directory when a unix socket is bound. In case someone want to fix the lock file path or use multiple gunicorn instance the "--lock-file" setting can be used to set the path of this file. fix #1259
- Loading branch information
Showing
5 changed files
with
118 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# -*- coding: utf-8 - | ||
# | ||
# This file is part of gunicorn released under the MIT license. | ||
# See the NOTICE for more information. | ||
|
||
import os | ||
import platform | ||
import tempfile | ||
|
||
from gunicorn import util | ||
|
||
PLATFORM = platform.system() | ||
IS_CYGWIN = PLATFORM.startswith('CYGWIN') | ||
|
||
if os.name == 'nt': | ||
import msvcrt | ||
|
||
def _lock(fd): | ||
msvcrt.locking(fd, msvcrt.LK_NBLCK, 1) | ||
|
||
|
||
def _unlock(fd): | ||
try: | ||
msvcrt.locking(fd, msvcrt.LK_UNLCK, 1) | ||
except OSError: | ||
return False | ||
return True | ||
|
||
else: | ||
import fcntl | ||
|
||
def _lock(fd): | ||
fcntl.lockf(fd, fcntl.LOCK_SH | fcntl.LOCK_NB) | ||
|
||
|
||
def _unlock(fd): | ||
try: | ||
fcntl.lockf(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) | ||
except: | ||
print("no unlock") | ||
return False | ||
|
||
return True | ||
|
||
|
||
class LockFile(object): | ||
"""Manage a LOCK file""" | ||
|
||
def __init__(self, cfg): | ||
self.fname = cfg.lockfile | ||
fdir = os.path.dirname(self.fname) | ||
if fdir and not os.path.isdir(fdir): | ||
raise RuntimeError("%s doesn't exist. Can't create lock file." % fdir) | ||
self._lockfile = open(self.fname, 'w+b') | ||
# set permissions to -rw-r--r-- | ||
os.chmod(self.fname, 420) | ||
|
||
def lock(self): | ||
_lock(self._lockfile.fileno()) | ||
|
||
def unlock(self): | ||
if not self.locked(): | ||
return | ||
|
||
if _unlock(self._lockfile.fileno()): | ||
self._lockfile.close() | ||
util.unlink(self.fname) | ||
self._lockfile = None | ||
|
||
def locked(self): | ||
return self._lockfile is not None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters