From c800140fab1cd94af2ba7e3185947ce685c7781d Mon Sep 17 00:00:00 2001 From: Almar Klein Date: Fri, 6 Dec 2024 09:51:36 +0100 Subject: [PATCH] use async in example --- examples/demo.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/examples/demo.py b/examples/demo.py index 6e3ea85..fa44aa9 100644 --- a/examples/demo.py +++ b/examples/demo.py @@ -8,15 +8,17 @@ * Can be closed with Escape or by pressing the window close button. * In both cases, it should print "Close detected" exactly once. -* Hit space to spend 2 seconds doing direct draws. +* Hit "f" to spend 2 seconds doing direct draws. +* Hit "s" to async-sleep the scheduling loop for 2 seconds. Resizing + and closing the window still work. """ import time -from rendercanvas.pyside6 import RenderCanvas, loop +from rendercanvas.glfw import RenderCanvas, loop from rendercanvas.utils.cube import setup_drawing_sync - +from rendercanvas.utils.asyncs import sleep canvas = RenderCanvas( size=(640, 480), @@ -39,13 +41,19 @@ async def process_event(event): if event["event_type"] == "key_down": if event["key"] == "Escape": canvas.close() - elif event["key"] == " ": + elif event["key"] in " f": + # Force draw for 2 secs + print("force-drawing ...") etime = time.time() + 2 i = 0 while time.time() < etime: i += 1 canvas.force_draw() - print(f"force-drawed {i} frames in 2s.") + print(f"Drew {i} frames in 2s.") + elif event["key"] == "s": + print("Async sleep ... zzzz") + await sleep(2) + print("waking up") elif event["event_type"] == "close": # Should see this exactly once, either when pressing escape, or # when pressing the window close button.