##// END OF EJS Templates
fixed lock decorator bug which didn't release the lock after func execution and rewrote the pidlock a little with Ask Solem suggestions!...
fixed lock decorator bug which didn't release the lock after func execution and rewrote the pidlock a little with Ask Solem suggestions! added tredning languages stats

File last commit:

r506:d5efb835 default
r506:d5efb835 default
Show More
pidlock.py
119 lines | 3.7 KiB | text/x-python | PythonLexer
Implemented locking for task, to prevent for running the same tasks,...
r497 import os, time
import sys
from warnings import warn
fixed lock decorator bug which didn't release the lock after func execution and rewrote the pidlock a little with Ask Solem suggestions!...
r506 from multiprocessing.util import Finalize
import errno
Implemented locking for task, to prevent for running the same tasks,...
r497
class LockHeld(Exception):pass
class DaemonLock(object):
"""daemon locking
USAGE:
try:
removed pidlock from whoosh and added it as locked_task decorator
r504 l = DaemonLock(desc='test lock')
Implemented locking for task, to prevent for running the same tasks,...
r497 main()
l.release()
except LockHeld:
sys.exit(1)
"""
def __init__(self, file=None, callbackfn=None,
desc='daemon lock', debug=False):
self.pidfile = file if file else os.path.join(os.path.dirname(__file__),
'running.lock')
self.callbackfn = callbackfn
self.desc = desc
self.debug = debug
self.held = False
#run the lock automatically !
self.lock()
fixed lock decorator bug which didn't release the lock after func execution and rewrote the pidlock a little with Ask Solem suggestions!...
r506 self._finalize = Finalize(self, DaemonLock._on_finalize,
args=(self, debug), exitpriority=10)
Implemented locking for task, to prevent for running the same tasks,...
r497
fixed lock decorator bug which didn't release the lock after func execution and rewrote the pidlock a little with Ask Solem suggestions!...
r506 @staticmethod
def _on_finalize(lock, debug):
if lock.held:
if debug:
print 'leck held finilazing and running lock.release()'
lock.release()
Implemented locking for task, to prevent for running the same tasks,...
r497
def lock(self):
removed pidlock from whoosh and added it as locked_task decorator
r504 """locking function, if lock is present it will raise LockHeld exception
Implemented locking for task, to prevent for running the same tasks,...
r497 """
lockname = '%s' % (os.getpid())
fixed lock decorator bug which didn't release the lock after func execution and rewrote the pidlock a little with Ask Solem suggestions!...
r506 if self.debug:
print 'running lock'
Implemented locking for task, to prevent for running the same tasks,...
r497 self.trylock()
self.makelock(lockname, self.pidfile)
return True
def trylock(self):
running_pid = False
fixed lock decorator bug which didn't release the lock after func execution and rewrote the pidlock a little with Ask Solem suggestions!...
r506 if self.debug:
print 'checking for already running process'
Implemented locking for task, to prevent for running the same tasks,...
r497 try:
pidfile = open(self.pidfile, "r")
pidfile.seek(0)
fixed lock decorator bug which didn't release the lock after func execution and rewrote the pidlock a little with Ask Solem suggestions!...
r506 running_pid = int(pidfile.readline())
pidfile.close()
Implemented locking for task, to prevent for running the same tasks,...
r497 if self.debug:
print 'lock file present running_pid: %s, checking for execution'\
% running_pid
# Now we check the PID from lock file matches to the current
# process PID
if running_pid:
fixed lock decorator bug which didn't release the lock after func execution and rewrote the pidlock a little with Ask Solem suggestions!...
r506 try:
os.kill(running_pid, 0)
except OSError, exc:
if exc.errno in (errno.ESRCH, errno.EPERM):
print "Lock File is there but the program is not running"
print "Removing lock file for the: %s" % running_pid
self.release()
raise
Implemented locking for task, to prevent for running the same tasks,...
r497 else:
fixed lock decorator bug which didn't release the lock after func execution and rewrote the pidlock a little with Ask Solem suggestions!...
r506 print "You already have an instance of the program running"
print "It is running as process %s" % running_pid
raise LockHeld()
Implemented locking for task, to prevent for running the same tasks,...
r497 except IOError, e:
if e.errno != 2:
raise
def release(self):
removed pidlock from whoosh and added it as locked_task decorator
r504 """releases the pid by removing the pidfile
Implemented locking for task, to prevent for running the same tasks,...
r497 """
fixed lock decorator bug which didn't release the lock after func execution and rewrote the pidlock a little with Ask Solem suggestions!...
r506 if self.debug:
print 'trying to release the pidlock'
Implemented locking for task, to prevent for running the same tasks,...
r497 if self.callbackfn:
#execute callback function on release
if self.debug:
print 'executing callback function %s' % self.callbackfn
self.callbackfn()
try:
if self.debug:
print 'removing pidfile %s' % self.pidfile
os.remove(self.pidfile)
self.held = False
except OSError, e:
if self.debug:
print 'removing pidfile failed %s' % e
pass
def makelock(self, lockname, pidfile):
"""
this function will make an actual lock
@param lockname: acctual pid of file
@param pidfile: the file to write the pid in
"""
if self.debug:
print 'creating a file %s and pid: %s' % (pidfile, lockname)
pidfile = open(self.pidfile, "wb")
pidfile.write(lockname)
pidfile.close
self.held = True