From 80be82544cf98165842f03ba4efdff0353f6a79c Mon Sep 17 00:00:00 2001 From: vviora Date: Fri, 17 Nov 2023 17:12:10 +0300 Subject: [PATCH] modified: noblocking_turtle_shell.py --- noblocking_turtle_shell.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/noblocking_turtle_shell.py b/noblocking_turtle_shell.py index 394fe37..4a95963 100644 --- a/noblocking_turtle_shell.py +++ b/noblocking_turtle_shell.py @@ -1,7 +1,7 @@ import cmd import threading -from queue import Queue +from queue import Queue, Empty from equipment.turtle_device import TurtleDevice @@ -13,6 +13,17 @@ class TurtleDeviceThread(threading.Thread): self.device = TurtleDevice() self.queue = Queue() + def run(self): + while True: + try: + item = self.queue.get() + except self.queue.Empty: + continue + else: + if (item == 'exit'): + break + self.device.execute(item[0], item[1:]) + self.queue.task_done() class NoBlockingTurtleShell(cmd.Cmd): intro = 'Welcome to the turtle shell. Type help or ? to list commands.\n' @@ -20,16 +31,16 @@ class NoBlockingTurtleShell(cmd.Cmd): file = None def __init__(self, turtle_thread: TurtleDeviceThread): - pass # TODO(Homework 4) + self.turtle_thread = TurtleDeviceThread() def do_execute(self, arg): - pass # TODO(Homework 4) + self.turtle_thread.queue.put(arg) def do_exit(self, arg): - pass # TODO(Homework 4) + self.turtle_thread.queue.put('exit') if __name__ == '__main__': turtle_thread = TurtleDeviceThread() - # TODO(Homework 4: Correct start thread) + turtle_thread.start() NoBlockingTurtleShell(turtle_thread).cmdloop() \ No newline at end of file