Test if a lock has been acquired in python
locking, python
Solution
There is no `locked` method - for good reasons.
if lock.locked():
lock.acquire()
lock.release()
If you do code like this, the state of the lock can change between the testing of the if condition and the acquire.
`acquire` allows non-blocking operation:
if lock.acquire(False):
#...
lock.release()
This will only execute the code inside the `if` if there's nothing else holding the lock - otherwise, it'll skip execution entirely.
Problem
I have Python 2.7.3. I am trying to use Lock object to allow only one instance of my script to run at a time. I am using: ``` from mutiprocessing import Lock lock = Lock() if lock.locked() == False: lock.acquire() lock.release() ``` as I have seen here But I am getting this error: ``` if lock.locked(): AttributeError: 'Lock' object has no attribute 'locked' ```