asyncio working

This commit is contained in:
Clément Le Bihan
2021-12-10 12:45:39 +01:00
parent 23cec55bfc
commit 8e7301c280
3 changed files with 19 additions and 15 deletions
+8 -10
View File
@@ -20,22 +20,20 @@ class Partition:
self.__notes = notes
def play(self, output_lambda:Callable[[str, tuple[int, int, int], int], None]):
async def play(self, output_lambda:Callable[[str, tuple[int, int, int], int], None]):
now = datetime.datetime.now()
tasks_to_wait = []
for note in self.__notes:
tasks_to_wait.append(
asyncio.create_task(
lambda: asyncio.wait(
run_at(
now + datetime.timedelta(milliseconds= note.get_start_time()),
output_lambda(
note.get_key(),
note.get_color(),
note.get_duration()
)
run_at(
now + datetime.timedelta(milliseconds= note.get_start_time()),
output_lambda(
note.get_key(),
note.get_color(),
note.get_duration()
)
)
)
)
asyncio.wait(tasks_to_wait)
await asyncio.wait(tasks_to_wait)
Binary file not shown.
+11 -5
View File
@@ -3,16 +3,22 @@ from chroma_case.Note import Note
import asyncio
import sys
def printing(key, color, duration):
async def printing(key, color, duration):
print(f"key: {key}, c:{color} for {duration / 1000}s")
await asyncio.sleep(duration / 1000)
print(f"end of {key}")
def main():
n = Note("fa", (0, 255, 0), 1000, 1000)
p = Partition("test", [n])
async def main():
n = Note("fa", (0, 255, 0), 1000, 7000)
p.play(printing)
p = Partition("test",
[n,
Note("fa#", (234, 255, 0), 3000, 2000)]
)
await p.play(printing)
return 0