From: W. Trevor King Date: Sun, 16 May 2010 13:29:51 +0000 (-0400) Subject: Pull Hooke.run*() multiprocessing setup/teardown into ._setup/_cleanup_run. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=07dda3e11adf448e31ce80a5dbf713960262a91c;p=hooke.git Pull Hooke.run*() multiprocessing setup/teardown into ._setup/_cleanup_run. Limits code duplication between .run() and .run_lines(). --- 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()