Python msvcrt.LK_NBLCK Examples

The following are 15 code examples of msvcrt.LK_NBLCK(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module msvcrt , or try the search function .
Example #1
Source File: filelock.py    From plugin.git.browser with GNU General Public License v3.0 6 votes vote down vote up
def _acquire(self):
        open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC

        try:
            fd = os.open(self._lock_file, open_mode)
        except OSError:
            pass
        else:
            try:
                msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
            except (IOError, OSError):
                try:
                    os.close(fd)
                except: 
                    fd.close()
            else:
                self._lock_file_fd = fd
        return None 
Example #2
Source File: glock.py    From ReynirPackage with GNU General Public License v3.0 6 votes vote down vote up
def _lock_file(file, block):
            # Lock just the first byte of the file
            retry = True
            while retry:
                retry = False
                try:
                    msvcrt.locking(
                        file.fileno(), msvcrt.LK_LOCK if block else msvcrt.LK_NBLCK, 1
                    )
                except OSError as e:
                    if block and e.errno == 36:
                        # Windows says 'resource deadlock avoided', but we truly want
                        # a longer blocking wait: try again
                        retry = True
                    else:
                        raise LockError(
                            "Couldn't lock {0}, errno is {1}".format(file.name, e.errno)
                        ) 
Example #3
Source File: filelock.py    From a4kScrapers with MIT License 5 votes vote down vote up
def _acquire(self):
        open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC

        try:
            fd = os.open(self._lock_file, open_mode)
        except OSError:
            pass
        else:
            try:
                msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
            except (IOError, OSError):
                os.close(fd)
            else:
                self._lock_file_fd = fd
        return None 
Example #4
Source File: process_lock.py    From fasteners with Apache License 2.0 5 votes vote down vote up
def _trylock(lockfile):
        fileno = lockfile.fileno()
        msvcrt.locking(fileno, msvcrt.LK_NBLCK, 1) 
Example #5
Source File: WindowsServer.py    From pycopia with Apache License 2.0 5 votes vote down vote up
def lock_exclusive(self, length, start=0, whence=0, nb=0):
        """Locking method compatible with Posix files."""
        if nb:
            mode = msvcrt.LK_NBLCK
        else:
            mode = msvcrt.LK_LOCK
        orig = self.tell()
        self.seek(start, whence)
        try:
            msvcrt.locking(self.fileno(), mode, length)
        finally:
            self.seek(orig) 
Example #6
Source File: filelock.py    From veros with MIT License 5 votes vote down vote up
def _acquire(self):
        open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC

        try:
            fd = os.open(self._lock_file, open_mode)
        except OSError:
            pass
        else:
            try:
                msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
            except (IOError, OSError):
                os.close(fd)
            else:
                self._lock_file_fd = fd
        return None 
Example #7
Source File: lock.py    From py-solc-x with MIT License 5 votes vote down vote up
def acquire(self, blocking):
        if not self._lock.acquire(blocking):
            return False
        while True:
            try:
                fd = os.open(self._lock_path, OPEN_MODE)
                msvcrt.locking(fd, msvcrt.LK_LOCK if blocking else msvcrt.LK_NBLCK, 1)
                self._fd = fd
                return True
            except OSError:
                if not blocking:
                    self._lock.release()
                    return False 
Example #8
Source File: filelock.py    From Jacinle with MIT License 5 votes vote down vote up
def _acquire(self):
        open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC

        try:
            fd = os.open(self._lock_file, open_mode)
        except OSError:
            pass
        else:
            try:
                msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
            except (IOError, OSError):
                os.close(fd)
            else:
                self._lock_file_fd = fd
        return None 
Example #9
Source File: file_lock.py    From pre-commit with MIT License 5 votes vote down vote up
def _locked(
            fileno: int,
            blocked_cb: Callable[[], None],
    ) -> Generator[None, None, None]:
        try:
            # TODO: https://github.com/python/typeshed/pull/3607
            msvcrt.locking(fileno, msvcrt.LK_NBLCK, _region)  # type: ignore
        except OSError:
            blocked_cb()
            while True:
                try:
                    # TODO: https://github.com/python/typeshed/pull/3607
                    msvcrt.locking(fileno, msvcrt.LK_LOCK, _region)  # type: ignore  # noqa: E501
                except OSError as e:
                    # Locking violation. Returned when the _LK_LOCK or _LK_RLCK
                    # flag is specified and the file cannot be locked after 10
                    # attempts.
                    if e.errno != errno.EDEADLOCK:
                        raise
                else:
                    break

        try:
            yield
        finally:
            # From cursory testing, it seems to get unlocked when the file is
            # closed so this may not be necessary.
            # The documentation however states:
            # "Regions should be locked only briefly and should be unlocked
            # before closing a file or exiting the program."
            # TODO: https://github.com/python/typeshed/pull/3607
            msvcrt.locking(fileno, msvcrt.LK_UNLCK, _region)  # type: ignore 
Example #10
Source File: filelock.py    From pyRevit with GNU General Public License v3.0 5 votes vote down vote up
def _acquire(self):
        open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC

        try:
            fd = os.open(self._lock_file, open_mode)
        except OSError:
            pass
        else:
            try:
                msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
            except (IOError, OSError):
                os.close(fd)
            else:
                self._lock_file_fd = fd
        return None 
Example #11
Source File: lockfile.py    From opsbro with MIT License 5 votes vote down vote up
def _lock_file(self):
        # Lock just the first byte
        try:
            msvcrt.locking(self.fp.fileno(), msvcrt.LK_NBLCK, 1)
        except IOError:
            raise LockError(self.fp.name) 
Example #12
Source File: test_lockutils.py    From oslo.concurrency with Apache License 2.0 5 votes vote down vote up
def lock_file(handle):
    if sys.platform == 'win32':
        msvcrt.locking(handle.fileno(), msvcrt.LK_NBLCK, 1)
    else:
        fcntl.flock(handle, fcntl.LOCK_EX | fcntl.LOCK_NB) 
Example #13
Source File: filelock.py    From py-filelock with The Unlicense 5 votes vote down vote up
def _acquire(self):
        open_mode = os.O_RDWR | os.O_CREAT | os.O_TRUNC

        try:
            fd = os.open(self._lock_file, open_mode)
        except OSError:
            pass
        else:
            try:
                msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
            except (IOError, OSError):
                os.close(fd)
            else:
                self._lock_file_fd = fd
        return None 
Example #14
Source File: lockfile.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def _lock_file(self):
        # Lock just the first byte
        try:
            msvcrt.locking(self.fp.fileno(), msvcrt.LK_NBLCK, 1)
        except IOError:
            raise LockError(self.fp.name) 
Example #15
Source File: __init__.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def _lock_file(file):
            # Lock just the first byte
            try:
                msvcrt.locking(file.fileno(), msvcrt.LK_NBLCK, 1)
            except IOError:
                raise LockError("Couldn't lock %r" % file.name)