Comments (4)
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.
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.
Is there a good way to clean up all the .lock files after without running something like os.remove?
from filelock.
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)
- typing-extensions is a dependency but is not needed at runtime HOT 5
- Bug: Reentrant behavior does not work with distinct lock objects on the same thread HOT 4
- Using filelock in docker HOT 1
- Allow disabling fcntl (and use softfilelock) with environment variable HOT 2
- Python 3.7 support dropoff at version 3.12.3 not reflected in 'Security' section HOT 1
- Request add `RwLock` HOT 1
- Add cancellation tokens support
- Demo usage in the documentation is uncommon and misleading HOT 2
- Deadlock when a process tries to open and lock on the same file twice HOT 5
- Suggestion on documentation improvement over SoftFileLock
- 6 tests fail HOT 6
- is_singleton=True with nested lock hangs HOT 1
- Release to PyPi is failing HOT 1
- AttributeError: '_CountedFileLock' object has no attribute 'thread_safe' HOT 2
- '_CountedFileLock' object has no attribute 'thread_safe' HOT 34
- the latest filelock package 3.15.1 has compatible issue with package virtualenv HOT 6
- Poetry build fails with new filelock bugfix version HOT 9
- TypeError: LockedJSONData.__init__() got an unexpected keyword argument 'lock_file' ; regression between 3.15.1 and 3.15.3 HOT 3
- Android/Termux doesn't have flock, use lockf, SoftFileLock, or no lock? HOT 1
- Add Fasteners to Similar libraries Documentation
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from filelock.