portlock
This library is considered stable.
Portlock
is a cross-process lock that is implemented with tcp
port binding.
Since no two processes could bind on a same TCP port.
Portlock
tries to bind 3 ports on loopback ip 127.0.0.1
.
If a Portlock
instance succeeds on binding 2 ports out of 3,
it is considered this instance has acquired the lock.
syntax:
portlock.PortlockError
Super class of all Portlock exceptions.
syntax:
portlock.PortlockTimeout
Timeout when waiting to acquire the lock.
syntax:
portlock.Portlock(key, timeout=1, sleep_time=None)
A lock instance.
Portlock is thread safe. It is OK to create just one lock in a process for all threads.
arguments:
-
key
: is a string as lock key.key
will be hashed to a certain port -
timeout
: is the max time in second to wait to acquire the lock.If
acquire()
exceedstimeout
, it raises anportlock.PortlockTimeout
exception. -
sleep_time
: is the time in second between every two attempts to bind a port.
syntax:
Portlock.acquire()
It tries to acquire the lock before timeout
.
return: nothing
syntax:
Portlock.release()
It releases the lock it holds, or does nothing if it does not hold the lock.
return: nothing
syntax:
Portlock.has_locked()
It checks if this instances has the lock.
return:
True
if it has the lock.
syntax:
with Portlock(key):
Portlock
supports with
statement.
When entering a with
statement of Portlock
instance it invokes acquire()
automatically.
And when leaving with
block, release()
will be called to release the lock.
Zhang Yanpo (张炎泼) [email protected]
The MIT License (MIT)
Copyright (c) 2015 Zhang Yanpo (张炎泼) [email protected]