From be5cc4bdc5ce75fa3896f37601dc97f7602b2a38 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Thu, 3 Dec 2015 03:36:02 +0100 Subject: Fix mulit-bot mode Spawn one process for each bot and kill all if one process dies (f.e. on KeyboardInterrupt) or if the parent process (Watcher) gets SIGTERM. --- __init__.py | 32 +++++++++++++++++--------------- phenny | 4 +--- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/__init__.py b/__init__.py index f537199..f157db7 100755 --- a/__init__.py +++ b/__init__.py @@ -13,10 +13,15 @@ import bot class Watcher(object): # Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735 def __init__(self): - self.child = os.fork() - if self.child != 0: - signal.signal(signal.SIGTERM, self.sig_term) - self.watch() + self.children = [] + signal.signal(signal.SIGTERM, self.sig_term) + + def add(self, config): + child = os.fork() + if child != 0: + self.children.append(child) + else: + run_phenny(config) def watch(self): try: os.wait() @@ -25,8 +30,9 @@ class Watcher(object): sys.exit() def kill(self): - try: os.kill(self.child, signal.SIGKILL) - except OSError: pass + for child in self.children: + try: os.kill(child, signal.SIGKILL) + except OSError: pass def sig_term(self, signum, frame): self.kill() @@ -41,10 +47,6 @@ def run_phenny(config): p = bot.Phenny(config) p.run(config.host, config.port, config.ssl) - try: Watcher() - except Exception, e: - print >> sys.stderr, 'Warning:', e, '(in __init__.py)' - while True: try: connect(config) except KeyboardInterrupt: @@ -57,11 +59,11 @@ def run_phenny(config): print >> sys.stderr, warning time.sleep(delay) -def run(config): - t = threading.Thread(target=run_phenny, args=(config,)) - if hasattr(t, 'run'): - t.run() - else: t.start() +def run(configs): + w = Watcher() + for config in configs: + w.add(config) + w.watch() if __name__ == '__main__': print __doc__ diff --git a/phenny b/phenny index 6406dda..5188a01 100755 --- a/phenny +++ b/phenny @@ -171,9 +171,7 @@ def main(argv=None): # Step Five: Initialise And Run The Phennies - # @@ ignore SIGHUP - for config_module in config_modules: - run(config_module) # @@ thread this + run(config_modules) if __name__ == '__main__': main() -- cgit v1.2.3-1-g7c22