GithubHelp home page GithubHelp logo

Comments (4)

wolph avatar wolph commented on September 23, 2024 1

Sorry about the slow response, I didn't see your reply earlier.

This is mostly a windows issue I believe, but if you use non-exclusive locking you should have the behaviour you're looking for.
When you're writing, lock the file, when you're reading, don't lock the file :)

from portalocker.

wolph avatar wolph commented on September 23, 2024

from portalocker.

CodingOctocat avatar CodingOctocat commented on September 23, 2024

Win11

I created a context manager:

FileLocker.py

def __enter__(self):
    self._file = open(self.filename, self._mode, **self._file_open_kwargs)
    portalocker.lock(self._file, flags=self._flags)
    return self._file

def __exit__(self, exc_type, exc_val, exc_tb):
    portalocker.unlock(self._file)

Usage:

with FileLocker('.lock', 'a+') as fp:
    fp.write('test')

Try read file:

with open('.lock', 'r') as fp:
    lines = fp.readlines()
    # raise PermissionError: [Errno 13] Permission denied

from portalocker.

CodingOctocat avatar CodingOctocat commented on September 23, 2024

To solve this problem, I changed the strategy so that the '.lock' file is only used to control locking, and another file ('.writelock') is used to read and write, so that only one process can write to the '.writelock' file at the same time, but the contents can be read at the same time.

from portalocker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.