From 07dda3e11adf448e31ce80a5dbf713960262a91c Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 16 May 2010 09:29:51 -0400 Subject: [PATCH] Pull Hooke.run*() multiprocessing setup/teardown into ._setup/_cleanup_run. Limits code duplication between .run() and .run_lines(). --- hooke/hooke.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/hooke/hooke.py b/hooke/hooke.py index e84ba88..72fab31 100644 --- a/hooke/hooke.py +++ b/hooke/hooke.py @@ -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() -- 2.26.2