Skip to content

Commit 9e25c73

Browse files
authored
Async event handlers are no longer allowed (#119)
1 parent 57cb5e2 commit 9e25c73

File tree

10 files changed

+59
-121
lines changed

10 files changed

+59
-121
lines changed

examples/demo.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99
* Can be closed with Escape or by pressing the window close button.
1010
* In both cases, it should print "Close detected" exactly once.
1111
* Hit "f" to spend 2 seconds doing direct draws.
12-
* Hit "s" to async-sleep the scheduling loop for 2 seconds. Resizing
13-
and closing the window still work.
1412
1513
"""
1614

1715
import time
1816

1917
from rendercanvas.auto import RenderCanvas, loop
2018
from rendercanvas.utils.cube import setup_drawing_sync
21-
from rendercanvas.utils.asyncs import sleep
2219
import rendercanvas
2320

2421

@@ -41,7 +38,7 @@
4138

4239

4340
@canvas.add_event_handler("*")
44-
async def process_event(event):
41+
def process_event(event):
4542
global cursor_index
4643

4744
if event["event_type"] not in ["pointer_move", "before_draw", "animate"]:
@@ -59,10 +56,6 @@ async def process_event(event):
5956
i += 1
6057
canvas.force_draw()
6158
print(f"Drew {i} frames in 2s.")
62-
elif event["key"] == "s":
63-
print("Async sleep ... zzzz")
64-
await sleep(2)
65-
print("waking up")
6659
elif event["key"] == "c":
6760
# Swap cursor
6861
shapes = list(rendercanvas.CursorShape)

rendercanvas/_events.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ def add_handler(self, *args, order: float = 0) -> Callable:
5050
5151
Arguments:
5252
callback (callable, optional): The event handler. Must accept a single event argument.
53-
Can be a plain function or a coroutine function.
54-
If you use async callbacks, see :ref:`async` for the limitations.
5553
*types (list of EventType): A list of event types.
5654
order (float): Set callback priority order. Callbacks with lower priorities
5755
are called first. Default is 0. When an event is emitted, callbacks with
@@ -113,6 +111,10 @@ def decorator(_callback: Callable):
113111
return decorator(callback)
114112

115113
def _add_handler(self, callback, order, *types):
114+
if iscoroutinefunction(callback):
115+
raise TypeError(
116+
"Coroutines cannot be callbacks (anymore). Instead, you can use a regular callback that calls `loop.add_task(coro)`."
117+
)
116118
if self._closed:
117119
return
118120
self.remove_handler(callback, *types)
@@ -166,7 +168,7 @@ def submit(self, event):
166168

167169
self._pending_events.append(event)
168170

169-
async def flush(self):
171+
def flush(self):
170172
"""Dispatch all pending events.
171173
172174
This should generally be left to the scheduler.
@@ -176,9 +178,9 @@ async def flush(self):
176178
event = self._pending_events.popleft()
177179
except IndexError:
178180
break
179-
await self.emit(event)
181+
self.emit(event)
180182

181-
async def emit(self, event):
183+
def emit(self, event):
182184
"""Directly emit the given event.
183185
184186
In most cases events should be submitted, so that they are flushed
@@ -192,15 +194,12 @@ async def emit(self, event):
192194
if event.get("stop_propagation", False):
193195
break
194196
with log_exception(f"Error during handling {event_type} event"):
195-
if iscoroutinefunction(callback):
196-
await callback(event)
197-
else:
198-
callback(event)
197+
callback(event)
199198
# Close?
200199
if event_type == "close":
201200
self._release()
202201

203-
async def close(self):
202+
def close(self):
204203
"""Close the event handler.
205204
206205
Drops all pending events, send the close event, and disables the emitter.
@@ -209,4 +208,4 @@ async def close(self):
209208
if not self._closed:
210209
self._pending_events.clear()
211210
self.submit({"event_type": "close"})
212-
await self.flush()
211+
self.flush()

rendercanvas/_loop.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async def _loop_task(self):
117117
closed_canvas_ids = set(event_emitters) - set(new_event_emitters)
118118
for canvas_id in closed_canvas_ids:
119119
events = event_emitters[canvas_id]
120-
await events.close()
120+
events.close()
121121

122122
# Keep canvases alive
123123
for canvas in canvases:

rendercanvas/_scheduler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ async def __scheduler_task(self):
143143
# Process events, handlers may request a draw
144144
if (canvas := self.get_canvas()) is None:
145145
break
146-
await canvas._process_events()
146+
canvas._process_events()
147147
del canvas
148148

149149
# Determine what to do next ...
@@ -176,7 +176,7 @@ async def __scheduler_task(self):
176176
if not do_draw:
177177
continue
178178

179-
await self._events.emit({"event_type": "before_draw"})
179+
self._events.emit({"event_type": "before_draw"})
180180

181181
# Ask the canvas to draw
182182
if (canvas := self.get_canvas()) is None:

rendercanvas/base.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,8 @@ def submit_event(self, event: dict) -> None:
308308

309309
# %% Scheduling and drawing
310310

311-
async def _process_events(self):
312-
"""Process events and animations (async).
311+
def _process_events(self):
312+
"""Process events and animations.
313313
314314
Called from the scheduler.
315315
"""
@@ -323,7 +323,7 @@ async def _process_events(self):
323323

324324
# Flush our events, so downstream code can update stuff.
325325
# Maybe that downstream code request a new draw.
326-
await self._events.flush()
326+
self._events.flush()
327327

328328
# TODO: implement later (this is a start but is not tested)
329329
# Schedule animation events until the lag is gone

rendercanvas/stub.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class StubRenderCanvas(BaseRenderCanvas):
7070
def _final_canvas_init(self):
7171
return super()._final_canvas_init()
7272

73-
async def _process_events(self):
74-
return await super()._process_events()
73+
def _process_events(self):
74+
return super()._process_events()
7575

7676
def _draw_frame_and_present(self):
7777
return super()._draw_frame_and_present()

tests/test_base.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,7 @@ def handler(event):
198198
c.submit_event({"event_type": "key_down", "value": 1})
199199
c.submit_event({"event_type": "key_down", "value": 2})
200200

201-
def sync_flush(events):
202-
coro = events.flush()
203-
while True:
204-
try:
205-
coro.send(None)
206-
except StopIteration:
207-
break
208-
209-
sync_flush(c._events)
201+
c._events.flush()
210202
assert events == [1, 2]
211203

212204

0 commit comments

Comments
 (0)