Python lockfile

From wikinotes

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)