GithubHelp home page GithubHelp logo

Question about filelock HOT 4 CLOSED

berkowitze avatar berkowitze commented on August 18, 2024
Question

from filelock.

Comments (4)

berkowitze avatar berkowitze commented on August 18, 2024

Not sure if this is Pythonic or even correct (I don't have too much experience with __enter__ and __exit__) but here's a potential solution:

class LockedFile(object):
    def __init__(self, filename, mode='r'):
        self._fn = filename
        self._mode = mode

    def __enter__(self):
        self._lock = FileLock('%s.lock' % self._fn)
        self._lock.acquire()
        self._f = open(self._fn, self._mode)
        return self._f

    def __exit__(self, a, b, c):
        self._lock.release()
        self._f.close()

with LockedFile('hello.txt', 'w'):
    print 'hello'

from filelock.

AdorablePotato avatar AdorablePotato commented on August 18, 2024

I'd use the contextmanager decorator, it's quite nifty.

from contextlib import contextmanager
from filelock import FileLock

@contextmanager
def locked_open(filename, mode):
        lock = FileLock(filename + ".lock")
        with lock, open(filename, mode) as f:
                yield f
        return None

with locked_open("my_file.txt", "w") as f:
        f.write("Hello")

with locked_open("my_file.txt", "r") as f:
        print(f.read())

I don't know your use case, but if you deal with a lot of files, you may want to try other, more efficient ways of interprocess communication.

from filelock.

berkowitze avatar berkowitze commented on August 18, 2024

Is there a good way to clean up all the .lock files after without running something like os.remove?

from filelock.

AdorablePotato avatar AdorablePotato commented on August 18, 2024

You could place them all inside a temporary directory and let the OS take care of that. This makes also sure, that they are not accidentally deleted when still in use.

https://docs.python.org/3.7/library/tempfile.html

from filelock.

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.