Revision 112d240d
b/lib/daemon.py | ||
---|---|---|
22 | 22 |
"""Module with helper classes and functions for daemons""" |
23 | 23 |
|
24 | 24 |
|
25 |
import asyncore |
|
25 | 26 |
import os |
26 | 27 |
import select |
27 | 28 |
import signal |
... | ... | |
40 | 41 |
"""Constructs a new Mainloop instance. |
41 | 42 |
|
42 | 43 |
""" |
43 |
self._io_wait = {} |
|
44 | 44 |
self._signal_wait = [] |
45 |
self._poller = select.poll() |
|
46 | 45 |
|
47 | 46 |
@utils.SignalHandled([signal.SIGCHLD]) |
48 | 47 |
@utils.SignalHandled([signal.SIGTERM]) |
... | ... | |
66 | 65 |
if stop_on_empty and not (self._io_wait): |
67 | 66 |
break |
68 | 67 |
|
69 |
# Wait for I/O events |
|
70 |
try: |
|
71 |
io_events = self._poller.poll(None) |
|
72 |
except select.error, err: |
|
73 |
# EINTR can happen when signals are sent |
|
74 |
if err.args and err.args[0] in (errno.EINTR,): |
|
75 |
io_events = None |
|
76 |
else: |
|
77 |
raise |
|
78 |
|
|
79 |
if io_events: |
|
80 |
# Check for I/O events |
|
81 |
for (evfd, evcond) in io_events: |
|
82 |
owner = self._io_wait.get(evfd, None) |
|
83 |
if owner: |
|
84 |
owner.OnIO(evfd, evcond) |
|
68 |
asyncore.loop(timeout=5, count=1, use_poll=True) |
|
85 | 69 |
|
86 | 70 |
# Check whether a signal was raised |
87 | 71 |
for sig in signal_handlers: |
... | ... | |
101 | 85 |
for owner in self._signal_wait: |
102 | 86 |
owner.OnSignal(signal.SIGCHLD) |
103 | 87 |
|
104 |
def RegisterIO(self, owner, fd, condition): |
|
105 |
"""Registers a receiver for I/O notifications |
|
106 |
|
|
107 |
The receiver must support a "OnIO(self, fd, conditions)" function. |
|
108 |
|
|
109 |
@type owner: instance |
|
110 |
@param owner: Receiver |
|
111 |
@type fd: int |
|
112 |
@param fd: File descriptor |
|
113 |
@type condition: int |
|
114 |
@param condition: ORed field of conditions to be notified |
|
115 |
(see select module) |
|
116 |
|
|
117 |
""" |
|
118 |
# select.Poller also supports file() like objects, but we don't. |
|
119 |
assert isinstance(fd, (int, long)), \ |
|
120 |
"Only integers are supported for file descriptors" |
|
121 |
|
|
122 |
self._io_wait[fd] = owner |
|
123 |
self._poller.register(fd, condition) |
|
124 |
|
|
125 | 88 |
def RegisterSignal(self, owner): |
126 | 89 |
"""Registers a receiver for signal notifications |
127 | 90 |
|
b/lib/http/server.py | ||
---|---|---|
30 | 30 |
import socket |
31 | 31 |
import time |
32 | 32 |
import signal |
33 |
import asyncore |
|
33 | 34 |
|
34 | 35 |
from ganeti import http |
35 | 36 |
|
... | ... | |
415 | 416 |
""" |
416 | 417 |
return self.error_message_format % values |
417 | 418 |
|
418 |
class HttpServer(http.HttpBase): |
|
419 |
class HttpServer(http.HttpBase, asyncore.dispatcher):
|
|
419 | 420 |
"""Generic HTTP server class |
420 | 421 |
|
421 | 422 |
Users of this class must subclass it and override the HandleRequest function. |
... | ... | |
445 | 446 |
|
446 | 447 |
""" |
447 | 448 |
http.HttpBase.__init__(self) |
449 |
asyncore.dispatcher.__init__(self) |
|
448 | 450 |
|
449 | 451 |
if request_executor_class is None: |
450 | 452 |
self.request_executor = HttpServerRequestExecutor |
... | ... | |
461 | 463 |
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
462 | 464 |
|
463 | 465 |
self._children = [] |
464 |
|
|
465 |
mainloop.RegisterIO(self, self.socket.fileno(), select.POLLIN)
|
|
466 |
self.set_socket(self.socket) |
|
467 |
self.accepting = True
|
|
466 | 468 |
mainloop.RegisterSignal(self) |
467 | 469 |
|
468 | 470 |
def Start(self): |
... | ... | |
472 | 474 |
def Stop(self): |
473 | 475 |
self.socket.close() |
474 | 476 |
|
475 |
def OnIO(self, fd, condition): |
|
476 |
if condition & select.POLLIN: |
|
477 |
self._IncomingConnection() |
|
477 |
def handle_accept(self): |
|
478 |
self._IncomingConnection() |
|
478 | 479 |
|
479 | 480 |
def OnSignal(self, signum): |
480 | 481 |
if signum == signal.SIGCHLD: |
Also available in: Unified diff