Skip to content

Commit

Permalink
use async in example
Browse files Browse the repository at this point in the history
  • Loading branch information
almarklein committed Dec 6, 2024
1 parent 2eadf4e commit c800140
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions examples/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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.
Expand Down

0 comments on commit c800140

Please sign in to comment.