diff options
author | Rob Austein <sra@hactrn.net> | 2009-05-04 19:51:25 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2009-05-04 19:51:25 +0000 |
commit | 8b02cc3b7a66caa972f2ff571fc52daec2088138 (patch) | |
tree | 68c336fd152a039b624ba0a7a3d8c7bc01988d12 | |
parent | 2271be2ab671d8d67602a6957d358ac5bebd9ad7 (diff) |
Move signal wrapper to rpki.async.event_loop().
svn path=/rpkid/rpki/async.py; revision=2397
-rw-r--r-- | rpkid/rpki/async.py | 25 | ||||
-rw-r--r-- | scripts/async-http.py | 18 |
2 files changed, 22 insertions, 21 deletions
diff --git a/rpkid/rpki/async.py b/rpkid/rpki/async.py index 12e45ffd..164e0102 100644 --- a/rpkid/rpki/async.py +++ b/rpkid/rpki/async.py @@ -18,7 +18,7 @@ OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. """ -import asyncore +import asyncore, signal import rpki.log, rpki.sundial class iterator(object): @@ -135,7 +135,7 @@ class timer(object): cls.queue.pop(0).handler() def __repr__(self): - return "<%s %s>" % (self.__class__.__name__, repr(self.when)) + return "<%s %r %r>" % (self.__class__.__name__, self.when, self.handler) @classmethod def seconds_until_wakeup(cls): @@ -167,8 +167,19 @@ class timer(object): while cls.queue: cls.queue.pop(0).cancel() -def event_loop(): - """Replacement for asyncore.loop(), adding timer support.""" - while asyncore.socket_map: - asyncore.poll(timer.seconds_until_wakeup(), asyncore.socket_map) - timer.runq() +def _raiseExitNow(signum, frame): + """Signal handler for event_loop().""" + raise asyncore.ExitNow + +def event_loop(catch_signals = (signal.SIGINT, signal.SIGTERM)): + """Replacement for asyncore.loop(), adding timer and signal support.""" + try: + old_signal_handlers = tuple((sig, signal.signal(sig, _raiseExitNow)) for sig in catch_signals) + while asyncore.socket_map or timer.queue: + asyncore.poll(timer.seconds_until_wakeup(), asyncore.socket_map) + timer.runq() + except asyncore.ExitNow: + pass + finally: + for sig, handler in old_signal_handlers: + signal.signal(sig, handler) diff --git a/scripts/async-http.py b/scripts/async-http.py index e4a366fb..5b5fc1cd 100644 --- a/scripts/async-http.py +++ b/scripts/async-http.py @@ -39,7 +39,7 @@ PERFORMANCE OF THIS SOFTWARE. # significantly depending on whether client signals HTTP 1.0 or 1.1; # the latter produces chunked output. -import sys, os, time, socket, asyncore, asynchat, traceback, urlparse, signal +import sys, os, time, socket, asyncore, asynchat, traceback, urlparse import rpki.async, rpki.sundial debug = True @@ -473,21 +473,11 @@ class http_manager(dict): def client(msg, url, timeout = 300, callback = None): pass -def server(handlers, port, host ="", catch_signals = (signal.SIGINT, signal.SIGTERM)): +def server(handlers, port, host =""): if not isinstance(handlers, (tuple, list)): handlers = (("/", handlers),) - try: - def raiseExitNow(signum, frame): - logger(None, "Signal received, shutting down") - raise asyncore.ExitNow - old_signal_handlers = tuple((sig, signal.signal(sig, raiseExitNow)) for sig in catch_signals) - listener = http_listener(port = 8000, handlers = handlers) - rpki.async.event_loop() - except asyncore.ExitNow: - pass - finally: - for sig, handler in old_signal_handlers: - signal.signal(sig, handler) + listener = http_listener(port = 8000, handlers = handlers) + rpki.async.event_loop() if len(sys.argv) == 1: |