Mercurial > kallithea
comparison pylons_app/lib/indexers/pidlock.py @ 406:b153a51b1d3b
Implemented search using whoosh. Still as experimental option.
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Tue, 17 Aug 2010 23:15:36 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
405:bec06654d67b | 406:b153a51b1d3b |
---|---|
1 import os, time | |
2 import sys | |
3 from warnings import warn | |
4 | |
5 class LockHeld(Exception):pass | |
6 | |
7 | |
8 class DaemonLock(object): | |
9 '''daemon locking | |
10 USAGE: | |
11 try: | |
12 l = lock() | |
13 main() | |
14 l.release() | |
15 except LockHeld: | |
16 sys.exit(1) | |
17 ''' | |
18 | |
19 def __init__(self, file=None, callbackfn=None, | |
20 desc='daemon lock', debug=False): | |
21 | |
22 self.pidfile = file if file else os.path.join(os.path.dirname(__file__), | |
23 'running.lock') | |
24 self.callbackfn = callbackfn | |
25 self.desc = desc | |
26 self.debug = debug | |
27 self.held = False | |
28 #run the lock automatically ! | |
29 self.lock() | |
30 | |
31 def __del__(self): | |
32 if self.held: | |
33 | |
34 # warn("use lock.release instead of del lock", | |
35 # category = DeprecationWarning, | |
36 # stacklevel = 2) | |
37 | |
38 # ensure the lock will be removed | |
39 self.release() | |
40 | |
41 | |
42 def lock(self): | |
43 ''' | |
44 locking function, if lock is present it will raise LockHeld exception | |
45 ''' | |
46 lockname = '%s' % (os.getpid()) | |
47 | |
48 self.trylock() | |
49 self.makelock(lockname, self.pidfile) | |
50 return True | |
51 | |
52 def trylock(self): | |
53 running_pid = False | |
54 try: | |
55 pidfile = open(self.pidfile, "r") | |
56 pidfile.seek(0) | |
57 running_pid = pidfile.readline() | |
58 if self.debug: | |
59 print 'lock file present running_pid: %s, checking for execution'\ | |
60 % running_pid | |
61 # Now we check the PID from lock file matches to the current | |
62 # process PID | |
63 if running_pid: | |
64 if os.path.exists("/proc/%s" % running_pid): | |
65 print "You already have an instance of the program running" | |
66 print "It is running as process %s" % running_pid | |
67 raise LockHeld | |
68 else: | |
69 print "Lock File is there but the program is not running" | |
70 print "Removing lock file for the: %s" % running_pid | |
71 self.release() | |
72 except IOError, e: | |
73 if e.errno != 2: | |
74 raise | |
75 | |
76 | |
77 def release(self): | |
78 ''' | |
79 releases the pid by removing the pidfile | |
80 ''' | |
81 if self.callbackfn: | |
82 #execute callback function on release | |
83 if self.debug: | |
84 print 'executing callback function %s' % self.callbackfn | |
85 self.callbackfn() | |
86 try: | |
87 if self.debug: | |
88 print 'removing pidfile %s' % self.pidfile | |
89 os.remove(self.pidfile) | |
90 self.held = False | |
91 except OSError, e: | |
92 if self.debug: | |
93 print 'removing pidfile failed %s' % e | |
94 pass | |
95 | |
96 def makelock(self, lockname, pidfile): | |
97 ''' | |
98 this function will make an actual lock | |
99 @param lockname: acctual pid of file | |
100 @param pidfile: the file to write the pid in | |
101 ''' | |
102 if self.debug: | |
103 print 'creating a file %s and pid: %s' % (pidfile, lockname) | |
104 pidfile = open(self.pidfile, "wb") | |
105 pidfile.write(lockname) | |
106 pidfile.close | |
107 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) |