Revision 69b99987 lib/daemon.py
b/lib/daemon.py | ||
---|---|---|
24 | 24 |
|
25 | 25 |
import asyncore |
26 | 26 |
import os |
27 |
import select |
|
28 | 27 |
import signal |
29 | 28 |
import errno |
30 | 29 |
import logging |
... | ... | |
158 | 157 |
class Mainloop(object): |
159 | 158 |
"""Generic mainloop for daemons |
160 | 159 |
|
160 |
@ivar scheduler: A sched.scheduler object, which can be used to register |
|
161 |
timed events |
|
162 |
|
|
161 | 163 |
""" |
162 | 164 |
def __init__(self): |
163 | 165 |
"""Constructs a new Mainloop instance. |
164 | 166 |
|
165 |
@ivar scheduler: A L{sched.scheduler} object, which can be used to register |
|
166 |
timed events |
|
167 |
|
|
168 | 167 |
""" |
169 | 168 |
self._signal_wait = [] |
170 | 169 |
self.scheduler = AsyncoreScheduler(time.time) |
171 | 170 |
|
172 | 171 |
@utils.SignalHandled([signal.SIGCHLD]) |
173 | 172 |
@utils.SignalHandled([signal.SIGTERM]) |
174 |
def Run(self, stop_on_empty=False, signal_handlers=None):
|
|
173 |
def Run(self, signal_handlers=None): |
|
175 | 174 |
"""Runs the mainloop. |
176 | 175 |
|
177 |
@type stop_on_empty: bool |
|
178 |
@param stop_on_empty: Whether to stop mainloop once all I/O waiters |
|
179 |
unregistered |
|
180 | 176 |
@type signal_handlers: dict |
181 | 177 |
@param signal_handlers: signal->L{utils.SignalHandler} passed by decorator |
182 | 178 |
|
... | ... | |
187 | 183 |
running = True |
188 | 184 |
# Start actual main loop |
189 | 185 |
while running: |
190 |
# Stop if nothing is listening anymore |
|
191 |
if stop_on_empty and not (self._io_wait): |
|
192 |
break |
|
193 |
|
|
194 | 186 |
if not self.scheduler.empty(): |
195 | 187 |
try: |
196 | 188 |
self.scheduler.run() |
... | ... | |
234 | 226 |
|
235 | 227 |
@type daemon_name: string |
236 | 228 |
@param daemon_name: daemon name |
237 |
@type optionparser: L{optparse.OptionParser}
|
|
229 |
@type optionparser: optparse.OptionParser
|
|
238 | 230 |
@param optionparser: initialized optionparser with daemon-specific options |
239 | 231 |
(common -f -d options will be handled by this module) |
240 |
@type options: object @param options: OptionParser result, should contain at |
|
241 |
least the fork and the debug options |
|
242 | 232 |
@type dirs: list of strings |
243 | 233 |
@param dirs: list of directories that must exist for this daemon to work |
244 | 234 |
@type check_fn: function which accepts (options, args) |
Also available in: Unified diff