Python lockfile

From wikinotes
Revision as of 13:26, 29 May 2016 by Will (talk | contribs) (Created page with "Takes care of all of your lockfile needs. Even compatible using a <code>with</code> statement. I found it easier to use after reading the sourcefiles. = Usage = <blockquote>...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Takes care of all of your lockfile needs. Even compatible using a with statement. I found it easier to use after reading the sourcefiles.

Usage

from   lockfile import pidlockfile
import time

lock = pidlockfile.PIDLockFile( '/home/will/dev/testlock.pid', threaded=True, timeout= 3 * 60 * 60 )


if lock.is_locked():
	pid = lock.read_pid()
	print 'lock is still in use by process: %s. Waiting for lock to be released' % pid

counter = 0
while lock.is_locked():
	time.sleep(1)
	if counter == 10:
		print 'still waiting for lock to be released...'
	counter +=1

with lock:
	while True:
		print 'running'
		time.sleep(1)