done turtle thread

This commit is contained in:
ilia 2023-11-16 21:21:10 +03:00
parent 6204c8e721
commit c524ddeb1a
6 changed files with 62 additions and 47 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
*.code-workspace
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files
__pycache__/ __pycache__/
*.py[cod] *.py[cod]

View File

@ -49,6 +49,6 @@ extensions = [
- [assignment](https://sciprogcentre.github.io/green-courses/pages/advanced_python/2023/hw/03.html) - [assignment](https://sciprogcentre.github.io/green-courses/pages/advanced_python/2023/hw/03.html)
- [solution](./turtle) - [solution](./turtle)
## Turtle Async (4th homework) ## Turtle Thread (4th homework)
- [assignment](https://sciprogcentre.github.io/green-courses/pages/advanced_python/2023/hw/04.html) - [assignment](https://sciprogcentre.github.io/green-courses/pages/advanced_python/2023/hw/04.html)
- [solution](./turtle_async) - [solution](./turtle)

11
turtle/README.md Normal file
View File

@ -0,0 +1,11 @@
# NonBlocking Turtle Shell
## Problem statement
Create a [non blocking shell](https://sciprogcentre.github.io/green-courses/pages/advanced_python/2023/hw/04.html) for Python Turtle.
## Test
I added two shortcut methods for testing:
- `le` = `execute left 1000` - spinning by 1000$^\circ$
- `fw` = `execute forward 100` - go forward by 100px
`le` takes a lot of time. The shell is not locked while turtle is spinning and you
can enter other commands (e.g. `fw`, `le`). The turtle will do them all
step-by-step.

View File

@ -69,7 +69,7 @@ class SynchronyDevice(Device):
@abstractmethod @abstractmethod
def execute(self, action_name: str, *args, **kwargs): def execute(self, action_name: str, *args, **kwargs):
"""Execute action `action_name`, using `args` and `kwargs` as action argument.""" """Execute action `action_name`, using `args` and `kwargs` as action argument."""
pass raise DeviceError
@abstractmethod @abstractmethod
def read(self, trait_name: str) -> Any: def read(self, trait_name: str) -> Any:

View File

@ -1,9 +1,6 @@
from turtle import Turtle from turtle import Turtle
from typing import Optional, Collection, Any from typing import Optional, Collection, Any
from controls.device import SynchronyDevice from controls.device import *
from controls.device import TraitDescriptor
from controls.device import ActionDescriptor
from controls.device import DeviceError
import inspect import inspect
@ -14,7 +11,7 @@ class TurtleDevice(SynchronyDevice):
super().open() super().open()
def close(self): def close(self):
self.turtle.clear() self.turtle.reset()
super().close() super().close()
def trait_descriptors(self) -> Collection[TraitDescriptor]: def trait_descriptors(self) -> Collection[TraitDescriptor]:
@ -47,8 +44,12 @@ class TurtleDevice(SynchronyDevice):
"""Execute action `action_name`, using `args` and `kwargs` as action argument.""" """Execute action `action_name`, using `args` and `kwargs` as action argument."""
action = self.get_descriptors()["actions"].get(action_name) action = self.get_descriptors()["actions"].get(action_name)
if action: if action:
return getattr(self.turtle, action_name)(*args, **kwargs) self._state = DeviceLifecycleState.CLOSE
super().execute() getattr(self.turtle, action_name)(*args, **kwargs)
self._state = DeviceLifecycleState.OPEN
return
else:
raise DeviceError("No such action: `{}`".format(action_name))
def invalidate(self, trait_name: str): def invalidate(self, trait_name: str):
"""Invalidate logical state of trait `trait_name`""" """Invalidate logical state of trait `trait_name`"""
@ -60,26 +61,27 @@ class TurtleDevice(SynchronyDevice):
descriptors = dict(actions=dict(), traits=dict()) descriptors = dict(actions=dict(), traits=dict())
for k, v in self.turtle.__dict__.items(): for k, v in self.turtle.__dict__.items():
if not k.startswith("_"): if not k.startswith("_"):
descriptors["traits"][k] = TraitDescriptor(k, descriptors["traits"][k] = TraitDescriptor(
inspect.getdoc(v), k, inspect.getdoc(v), readable=True, writable=False
readable=True, )
writable=False)
for m_name, member in inspect.getmembers(Turtle): for m_name, member in inspect.getmembers(Turtle):
if m_name.startswith("_"): continue if m_name.startswith("_"):
if m_name.lower() != m_name: continue continue
if m_name.lower() != m_name:
continue
doc = inspect.getdoc(member) doc = inspect.getdoc(member)
if doc is None: continue if doc is None:
continue
if not inspect.isfunction(member): if not inspect.isfunction(member):
descriptors["traits"][m_name] = TraitDescriptor(m_name, descriptors["traits"][m_name] = TraitDescriptor(
doc, m_name, doc, readable=True, writable=False
readable=True, )
writable=False)
else: else:
sig = inspect.signature(member) sig = inspect.signature(member)
params_dict = dict(sig.parameters) params_dict = dict(sig.parameters)
descriptors["actions"][m_name] = ActionDescriptor(m_name, descriptors["actions"][m_name] = ActionDescriptor(
arguments=params_dict, m_name, arguments=params_dict, info=doc
info=doc) )
return descriptors return descriptors

View File

@ -3,7 +3,6 @@ import threading
from queue import Queue from queue import Queue
import turtle
from equipment.turtle_device import TurtleDevice from equipment.turtle_device import TurtleDevice
@ -12,6 +11,7 @@ class TurtleDeviceThread(threading.Thread):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.device = TurtleDevice() self.device = TurtleDevice()
self.queue = Queue[tuple]() self.queue = Queue[tuple]()
self.device.open() self.device.open()
@ -20,6 +20,9 @@ class TurtleDeviceThread(threading.Thread):
def run(self): def run(self):
while True: while True:
action, args, kwargs = self.queue.get() action, args, kwargs = self.queue.get()
if action == "exit":
self.queue.task_done()
break
self.device.execute(action, *args, **kwargs) self.device.execute(action, *args, **kwargs)
self.queue.task_done() self.queue.task_done()
@ -41,33 +44,30 @@ class NoBlockingTurtleShell(cmd.Cmd):
return self.turtle_thread.device return self.turtle_thread.device
def do_execute(self, arg): def do_execute(self, arg):
print(arg) command_and_args = arg.split()
command, number = tuple(arg.split()) if len(command_and_args) == 1:
assert number.isdecimal() self.turtle_thread.add_task(command)
self.turtle_thread.add_task(command, int(number)) return
command = command_and_args[0]
args = tuple(int(c) if c.isdecimal() else c for c in command_and_args[1:])
self.turtle_thread.add_task(command, *args)
def do_fw(self, arg):
self.do_execute("forward 100")
def do_le(self, arg):
self.do_execute("left 1000")
def do_exit(self, arg): def do_exit(self, arg):
self.turtle_device.close() self.turtle_thread.add_task("exit")
self.close() return True
def precmd(self, line):
line = line.lower()
if self.file and "playback" not in line:
print(line, file=self.file)
return line
def close(self):
if self.file:
self.file.close()
self.file = None
import tkinter
if __name__ == "__main__": if __name__ == "__main__":
turtle_thread = TurtleDeviceThread() turtle_thread = TurtleDeviceThread()
# TODO(Homework 4: Correct start thread) # TODO(Homework 4: Correct start thread)
turtle_thread.daemon = True turtle_thread.daemon = True
turtle_thread.start() thread_shell = threading.Thread(target=NoBlockingTurtleShell(turtle_thread).cmdloop)
NoBlockingTurtleShell(turtle_thread).cmdloop() thread_shell.start()
turtle_thread.join() turtle_thread.run()
thread_shell.join()