Comments (3)
I agree, it can definitely be improved :)
Also is there anyway to update the lock from one flags to another without unlocking?
Not as far as I am aware, but if you are using a shared lock you could create a new lock in addition. I doubt it's useful that way however...
from portalocker.
Don't worry I ended up using writing to another file and waiting 0.1 seconds in the additional instances, here's what I have done thank to you.
def ensure_single_instance(is_debug=False):
file = open(LOCK_FILENAME, 'w+', encoding='utf-8')
# no old running instances found, try locking file
try:
# exclusively locked
portalocker.lock(file, portalocker.LockFlags.EXCLUSIVE | portalocker.LockFlags.NON_BLOCKING)
create_pid_file()
if is_debug:
print(f'Locked {LOCK_FILENAME} pid = {os.getpid()}')
except LockException:
# another instance is probably running
# wait a bit for pid to be written to file
time.sleep(0.1)
pid, port = parse_pid_file()
look_for = 'Music Caster' if IS_FROZEN else 'python'
# double check if it's already running
# if more than one instance, there's definitely >3 processes
threshold = 3 if pid is None else 0
if is_already_running(threshold=threshold, look_for=look_for, pid=pid):
if is_debug:
print('not exiting because we are DEBUGGING')
else:
activate_instance(port=port, timeout=5)
sys.exit()
else:
print('instance not found, lock broken?')
return file
from portalocker.
Related Issues (20)
- Rlock - with limit? HOT 6
- What are the different locking mechanisms? HOT 2
- Fix `ResourceWarning` when `AlreadyLocked` HOT 6
- Declare PEP 561 compliance (type checking support) HOT 3
- Sorry for OT HOT 2
- Neither exclusive nor shared locks working as expected on Windows HOT 2
- Windows shared lock with Python 3 HOT 11
- redis submodule cannot be imported due to circular import HOT 4
- race condition for⦠file metadata? HOT 4
- Setting the DenyMode against a file HOT 2
- is lock reenterable? HOT 2
- distutils was depreacted in Python 3.10 HOT 2
- Timeout not working with SHARED flag HOT 4
- LockException(OSError(22, 'Invalid argument')) when using NON_BLOCKING flag on Linux HOT 2
- DLL load failed while importing win32file HOT 1
- What is the actual license for this project? HOT 1
- fileno support HOT 2
- Mismatch between documentation and implementation HOT 3
- How to lock a file but be able to read it HOT 4
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 portalocker.