Pull Hooke.run*() multiprocessing setup/teardown into ._setup/_cleanup_run.
authorW. Trevor King <wking@drexel.edu>
Sun, 16 May 2010 13:29:51 +0000 (09:29 -0400)
committerW. Trevor King <wking@drexel.edu>
Sun, 16 May 2010 13:29:51 +0000 (09:29 -0400)
Limits code duplication between .run() and .run_lines().

hooke/hooke.py

index e84ba88f481525863a7d86197ec4488f84babec9..72fab311d59bd49f50c0ae682e7a4eaf29189e1d 100644 (file)
@@ -80,16 +80,11 @@ class Hooke (object):
         then runs the UI, rejoining the `CommandEngine` process after
         the UI exits.
         """
-        ui_to_command = multiprocessing.Queue()
-        command_to_ui = multiprocessing.Queue()
-        command = multiprocessing.Process(
-            target=self.command.run, args=(self, ui_to_command, command_to_ui))
-        command.start()
+        ui_to_command,command_to_ui,command = self._setup_run()
         try:
             self.ui.run(self.commands, ui_to_command, command_to_ui)
         finally:
-            ui_to_command.put(ui.CloseEngine())
-            command.join()
+            self._cleanup_command(ui_to_command, command_to_ui, command)
 
     def run_lines(self, lines):
         """Run the pre-set commands `lines` with the "command line" UI.
@@ -98,17 +93,25 @@ class Hooke (object):
         equivalent to :meth:'.run'.
         """
         cmdline = ui.load_ui(self.config, 'command line')
-        ui_to_command = multiprocessing.Queue()
-        command_to_ui = multiprocessing.Queue()
-        command = multiprocessing.Process(
-            target=self.command.run, args=(self, ui_to_command, command_to_ui))
-        command.start()
+        ui_to_command,command_to_ui,command = self._setup_run()
         try:
             cmdline.run_lines(
                 self.commands, ui_to_command, command_to_ui, lines)
         finally:
-            ui_to_command.put(ui.CloseEngine())
-            command.join()
+            self._cleanup_command(ui_to_command, command_to_ui, command)
+
+    def _setup_run(self):
+        ui_to_command = multiprocessing.Queue()
+        command_to_ui = multiprocessing.Queue()
+        command = multiprocessing.Process(name='command engine',
+            target=self.command.run, args=(self, ui_to_command, command_to_ui))
+        command.start()
+        return (ui_to_command, command_to_ui, command)
+
+    def _cleanup_run(self, ui_to_command, command_to_ui, command):
+        ui_to_command.put(ui.CloseEngine())
+        command.join()
+
 
 def main():
     p = optparse.OptionParser()