��# Stome
��
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import asyncio | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class Run(ABC): | ||
|
||
def __init__(self, event): | ||
self._name = self.__class__.__name__ | ||
self._event = event | ||
|
||
async def __aenter__(self): | ||
print(f"Initiating {self._name, self._event}") | ||
return self | ||
|
||
async def __aexit__(self, exc_type, exc, tb): | ||
if exc_type is not None: | ||
print(f"Exception {exc_type} in {self._name}. {exc}") | ||
return True | ||
|
||
async def return_value(self, event): | ||
print(f"{'True' if event.is_set() else 'False'} from {self._name}") | ||
await asyncio.sleep(1) | ||
|
||
@abstractmethod | ||
def task(self, event): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from Stome import Run | ||
import asyncio | ||
import msvcrt | ||
import keyboard | ||
|
||
|
||
class Test1(Run): | ||
|
||
async def task(self, event): | ||
while True: | ||
await self.return_value(event) | ||
await event.wait() | ||
|
||
|
||
class Test2(Run): | ||
|
||
async def task(self, event): | ||
while True: | ||
await self.return_value(event) | ||
await event.wait() | ||
|
||
|
||
class Key(Run): | ||
|
||
async def task(self, event): | ||
while True: | ||
is_pressed = keyboard.is_pressed(hotkey="insert") | ||
if is_pressed and not _was_pressed: | ||
event.clear() if event.is_set() else event.set() | ||
print("Switch state:", event.is_set()) | ||
|
||
_was_pressed = is_pressed | ||
await asyncio.sleep(0.1) | ||
|
||
|
||
async def main(): | ||
halt = asyncio.Event() | ||
subclasses = [globals()[index.__name__] for index in Run.__subclasses__()] | ||
|
||
async with asyncio.TaskGroup() as group: | ||
for Class in subclasses: | ||
instance = Class(halt) | ||
async with instance: | ||
group.create_task(instance.task(halt)) | ||
|
||
|
||
try: | ||
asyncio.run(main()) | ||
except KeyboardInterrupt: | ||
pass | ||
finally: | ||
while msvcrt.kbhit(): | ||
msvcrt.getch() |