deduplicate spawn_async invocations
authorHelmut Grohne <helmut@subdivi.de>
Mon, 17 Jun 2013 11:32:49 +0000 (13:32 +0200)
committerHelmut Grohne <helmut@subdivi.de>
Mon, 17 Jun 2013 11:32:49 +0000 (13:32 +0200)
onoff/command.py
onoff/gobject.py [new file with mode: 0644]
onoff/process.py

index c3dd4a6..f6b6c9b 100644 (file)
@@ -1,8 +1,7 @@
 import logging
 
-from gi.repository import GObject
-
 from .common import OnoffDevice, ST_ACTIVE, ST_TRANSITION
+from .gobject import spawn_child
 
 logger = logging.getLogger("onoff.command")
 
@@ -27,11 +26,8 @@ class OnoffCommand(OnoffDevice):
                          (self.oncommand, "oncommand")][state]
         self.target_state = state
         logger.info("invoking %s %s", name, " ".join(command))
-        ret = GObject.spawn_async(command, flags=GObject.SPAWN_SEARCH_PATH | GObject.SPAWN_DO_NOT_REAP_CHILD)
-        self.pid = ret[0]
-        assert self.pid
+        self.pid, self.watch = spawn_child(command, self.process_died)
         logger.debug("started %s as pid %d", name, self.pid)
-        self.watch = GObject.child_watch_add(self.pid, self.process_died)
         self.changestate(self.state)
 
     def process_died(self, pid, condition):
diff --git a/onoff/gobject.py b/onoff/gobject.py
new file mode 100644 (file)
index 0000000..ea98019
--- /dev/null
@@ -0,0 +1,18 @@
+from gi.repository import GObject
+
+def spawn_child(command, callback):
+    """
+    @type command: [str]
+    @param command: an argument vector. First element is used as argv[0] and
+            used as executable to look up in $PATH.
+    @type callback: (pid, condition) -> None
+    @param callback: is executed when the command completes
+    @rtype: (int, int)
+    @returns: the child process pid and the event id of the callback
+    """
+    ret = GObject.spawn_async(command,
+            flags=GObject.SPAWN_SEARCH_PATH | GObject.SPAWN_DO_NOT_REAP_CHILD)
+    pid = ret[0]
+    assert pid
+    watch = GObject.child_watch_add(pid, callback)
+    return pid, watch
index aca7c4d..d888842 100644 (file)
@@ -5,6 +5,7 @@ import signal
 from gi.repository import GObject
 
 from .common import ST_ACTIVE, ST_TRANSITION, OnoffDevice
+from .gobject import spawn_child
 
 logger = logging.getLogger("onoff.process")
 
@@ -30,11 +31,8 @@ class OnoffProcess(OnoffDevice):
         assert self.pid is None
         assert self.starting is None
         logger.info("starting command %s", " ".join(self.command))
-        ret = GObject.spawn_async(self.command, flags=GObject.SPAWN_SEARCH_PATH | GObject.SPAWN_DO_NOT_REAP_CHILD)
-        self.pid = ret[0]
-        assert self.pid
+        self.pid, self.watch = spawn_child(self.command, self.process_died)
         logger.debug("started as pid %d", self.pid)
-        self.watch = GObject.child_watch_add(self.pid, self.process_died)
         self.starting = GObject.timeout_add(1000 * self.start_wait,
                                             self.process_started)
         self.changestate(self.state)