If the UI closes before the last command finishes (e.g. if bin/hooke
is run in a pipe), there may be cruft (e.g. CommandExit instances)
left in the engine-to-UI queue before the Hooke instance that the
CommandEngine appends on closing. The new implementation keeps
digging through the queue until it finds a Hooke instance, where the
old implementation only looked at the first message that came off the
queue.
Also added some useful debugging logs for UI<->Engine communication.
:class:`hooke.command.Command`\s.
"""
:class:`hooke.command.Command`\s.
"""
from .ui import CloseEngine, CommandMessage
from .ui import CloseEngine, CommandMessage
class CommandEngine (object):
def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
"""Get a :class:`hooke.ui.QueueMessage` from the incoming
class CommandEngine (object):
def run(self, hooke, ui_to_command_queue, command_to_ui_queue):
"""Get a :class:`hooke.ui.QueueMessage` from the incoming
`command_to_ui_queue`, at which point the `CommandEngine` will
be ready to receive the next :class:`hooke.ui.QueueMessage`.
"""
`command_to_ui_queue`, at which point the `CommandEngine` will
be ready to receive the next :class:`hooke.ui.QueueMessage`.
"""
+ log = logging.getLogger('hooke')
+ log.debug('engine waiting for command')
msg = ui_to_command_queue.get()
if isinstance(msg, CloseEngine):
command_to_ui_queue.put(hooke)
msg = ui_to_command_queue.get()
if isinstance(msg, CloseEngine):
command_to_ui_queue.put(hooke)
+ log.debug(
+ 'engine closing, placed hooke instance in return queue')
break
assert isinstance(msg, CommandMessage), type(msg)
break
assert isinstance(msg, CommandMessage), type(msg)
+ log.debug('engine running %s' % msg.command.name)
msg.command.run(hooke, ui_to_command_queue, command_to_ui_queue,
**msg.arguments)
msg.command.run(hooke, ui_to_command_queue, command_to_ui_queue,
**msg.arguments)
import multiprocessing
import optparse
import os.path
import multiprocessing
import optparse
import os.path
import unittest
import StringIO
import sys
import unittest
import StringIO
import sys
return (ui_to_command, command_to_ui, command)
def _cleanup_run(self, ui_to_command, command_to_ui, command):
return (ui_to_command, command_to_ui, command)
def _cleanup_run(self, ui_to_command, command_to_ui, command):
+ log = logging.getLogger('hooke')
+ log.debug('cleanup sending CloseEngine')
ui_to_command.put(ui.CloseEngine())
ui_to_command.put(ui.CloseEngine())
- hooke = command_to_ui.get()
- assert isinstance(hooke, Hooke)
+ hooke = None
+ while not isinstance(hooke, Hooke):
+ log.debug('cleanup waiting for Hooke instance from the engine.')
+ hooke = command_to_ui.get(block=True)
+ log.debug('cleanup got %s instance' % type(hooke))
command.join()
return hooke
command.join()
return hooke