diff options
author | Rob Austein <sra@hactrn.net> | 2009-06-27 19:00:52 +0000 |
---|---|---|
committer | Rob Austein <sra@hactrn.net> | 2009-06-27 19:00:52 +0000 |
commit | 603dc8f93d32378242bcb43670656bef61b2754e (patch) | |
tree | d3c10363d15c42a43c6d0e7577d09a04e98eb036 /rpkid/rpki/async.py | |
parent | 9137ed8958dfe1362f22909cd8f04463c153a1c8 (diff) |
sync_wrapper()
svn path=/rpkid/rpki/async.py; revision=2549
Diffstat (limited to 'rpkid/rpki/async.py')
-rw-r--r-- | rpkid/rpki/async.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/rpkid/rpki/async.py b/rpkid/rpki/async.py index 53c9e4b2..18ed2a2e 100644 --- a/rpkid/rpki/async.py +++ b/rpkid/rpki/async.py @@ -233,6 +233,39 @@ def event_loop(catch_signals = (signal.SIGINT, signal.SIGTERM)): for sig in old_signal_handlers: signal.signal(sig, old_signal_handlers[sig]) +class sync_wrapper(object): + """ + Synchronous wrapper around asynchronous functions. Running in + asynchronous mode at all times makes sense for event-driven daemons, + but is kind of tedious for simple scripts, hence this wrapper. + + The wrapped function should take at least two arguments: a callback + function and an errback function. If any arguments are passed to + the wrapper, they will be passed as additional arguments to the + wrapped function. + """ + + res = None + err = None + + def __init__(self, func): + self.func = func + + def cb(self, res = None): + self.res = res + raise ExitNow + + def eb(self, err): + self.err = err + raise ExitNow + + def __call__(self, *args, **kwargs): + timer(lambda: self.func(self.cb, self.eb, *args, **kwargs), self.eb).set(None) + event_loop() + if self.err is not None: + raise self.err + return self.res + def exit_event_loop(): """Force exit from event_loop().""" raise ExitNow |