[Pkg-privacy-commits] [pyptlib] 77/136: add a cleanup phase to killall() so that subproc plays nicely with Twisted and other programs that don't auto-shutdown

Ximin Luo infinity0 at moszumanska.debian.org
Sat Aug 22 13:25:11 UTC 2015


This is an automated email from the git hooks/post-receive script.

infinity0 pushed a commit to branch master
in repository pyptlib.

commit f1109ef3fff4325fcee661ff7d13b61ead62da5c
Author: Ximin Luo <infinity0 at gmx.com>
Date:   Thu Aug 15 15:23:36 2013 +0100

    add a cleanup phase to killall() so that subproc plays nicely with Twisted and other programs that don't auto-shutdown
---
 pyptlib/test/util_subproc_main.py |  2 +-
 pyptlib/util/subproc.py           | 17 +++++++++++++----
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/pyptlib/test/util_subproc_main.py b/pyptlib/test/util_subproc_main.py
index 16ad795..f2ead66 100644
--- a/pyptlib/test/util_subproc_main.py
+++ b/pyptlib/test/util_subproc_main.py
@@ -54,7 +54,7 @@ def main_trap_sigint_reset(testname, *argv):
 def main_killall_kill(testname, *argv):
     child = startChild(testname, True)
     time.sleep(1)
-    killall(4)
+    killall(wait_s=4)
     time.sleep(100)
 
 def main_auto_killall_2_int(testname, *argv):
diff --git a/pyptlib/util/subproc.py b/pyptlib/util/subproc.py
index 25c6d6d..314441c 100644
--- a/pyptlib/util/subproc.py
+++ b/pyptlib/util/subproc.py
@@ -145,12 +145,19 @@ def _run_sigint_handlers(signum=0, sframe=None):
 
 
 _isTerminating = False
-def killall(wait_s=16):
+def killall(cleanup=lambda:None, wait_s=16):
     """Attempt to gracefully terminate all child processes.
 
     All children are told to terminate gracefully. A waiting period is then
     applied, after which all children are killed forcefully. If all children
     terminate before this waiting period is over, the function exits early.
+
+    Args:
+        cleanup: Run after all children are dead. For example, if your program
+                does not automatically terminate after this, you can use this
+                to signal that it should exit. In particular, Twisted
+                applications ought to use this to call reactor.stop().
+        wait_s: Time in seconds to wait before trying to kill children.
     """
     # TODO(infinity0): log this somewhere, maybe
     global _isTerminating, _CHILD_PROCS
@@ -174,16 +181,18 @@ def killall(wait_s=16):
     # reap any zombies
     for proc in _CHILD_PROCS:
         proc.poll()
+    cleanup()
 
-def auto_killall(ignoreNumSigInts=0):
+def auto_killall(ignoreNumSigInts=0, *args, **kwargs):
     """Automatically terminate all child processes on exit.
 
     Args:
         ignoreNumSigInts: this number of INT signals will be ignored before
             attempting termination. This will be attempted unconditionally in
             all other cases, such as on normal exit, or on a TERM signal.
+        *args, **kwargs: See killall().
     """
-    killall_handler = lambda signum, sframe: killall()
+    killall_handler = lambda signum, sframe: killall(*args, **kwargs)
     trap_sigint(killall_handler, ignoreNumSigInts)
     signal.signal(signal.SIGTERM, killall_handler)
-    atexit.register(killall)
+    atexit.register(killall, *args, **kwargs)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/pyptlib.git



More information about the Pkg-privacy-commits mailing list