comparison pylons_app/lib/pidlock.py @ 504:d280aa1c85c6

removed pidlock from whoosh and added it as locked_task decorator
author Marcin Kuzminski <marcin@python-works.com>
date Sat, 25 Sep 2010 20:17:56 +0200
parents fb0c3af6031b
children d5efb83590ef
comparison
equal deleted inserted replaced
503:3d6d548ad3cc 504:d280aa1c85c6
7 7
8 class DaemonLock(object): 8 class DaemonLock(object):
9 """daemon locking 9 """daemon locking
10 USAGE: 10 USAGE:
11 try: 11 try:
12 l = lock() 12 l = DaemonLock(desc='test lock')
13 main() 13 main()
14 l.release() 14 l.release()
15 except LockHeld: 15 except LockHeld:
16 sys.exit(1) 16 sys.exit(1)
17 """ 17 """
38 # ensure the lock will be removed 38 # ensure the lock will be removed
39 self.release() 39 self.release()
40 40
41 41
42 def lock(self): 42 def lock(self):
43 """ 43 """locking function, if lock is present it will raise LockHeld exception
44 locking function, if lock is present it will raise LockHeld exception
45 """ 44 """
46 lockname = '%s' % (os.getpid()) 45 lockname = '%s' % (os.getpid())
47 46
48 self.trylock() 47 self.trylock()
49 self.makelock(lockname, self.pidfile) 48 self.makelock(lockname, self.pidfile)
73 if e.errno != 2: 72 if e.errno != 2:
74 raise 73 raise
75 74
76 75
77 def release(self): 76 def release(self):
78 """ 77 """releases the pid by removing the pidfile
79 releases the pid by removing the pidfile
80 """ 78 """
81 if self.callbackfn: 79 if self.callbackfn:
82 #execute callback function on release 80 #execute callback function on release
83 if self.debug: 81 if self.debug:
84 print 'executing callback function %s' % self.callbackfn 82 print 'executing callback function %s' % self.callbackfn
103 print 'creating a file %s and pid: %s' % (pidfile, lockname) 101 print 'creating a file %s and pid: %s' % (pidfile, lockname)
104 pidfile = open(self.pidfile, "wb") 102 pidfile = open(self.pidfile, "wb")
105 pidfile.write(lockname) 103 pidfile.write(lockname)
106 pidfile.close 104 pidfile.close
107 self.held = True 105 self.held = True
108
109
110 def main():
111 print 'func is running'
112 cnt = 20
113 while 1:
114 print cnt
115 if cnt == 0:
116 break
117 time.sleep(1)
118 cnt -= 1
119
120
121 if __name__ == "__main__":
122 try:
123 l = DaemonLock(desc='test lock')
124 main()
125 l.release()
126 except LockHeld:
127 sys.exit(1)