Skip to content

Commit 14bb200

Browse files
authored
Detect Pyodide to avoid get_asyncgen_hooks (#179)
1 parent cd877db commit 14bb200

4 files changed

Lines changed: 12 additions & 3 deletions

File tree

rendercanvas/_coreutils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020

2121
IS_WIN = sys.platform.startswith("win") # Note that IS_WIN is false on Pyodide
22+
IS_PYODIDE = sys.platform == "emscripten"
2223

2324

2425
# %% Logging

rendercanvas/utils/asyncs.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import sys
1414

15-
from .._coreutils import IS_WIN, call_later_from_thread
15+
from .._coreutils import IS_WIN, IS_PYODIDE, call_later_from_thread
1616

1717

1818
USE_THREADED_TIMER = IS_WIN
@@ -24,6 +24,10 @@
2424

2525
def detect_current_async_lib():
2626
"""Get the lib name of the currently active async lib, or None."""
27+
28+
if IS_PYODIDE:
29+
return "asyncio"
30+
2731
ob = sys.get_asyncgen_hooks()[0]
2832
if ob is not None:
2933
try:
@@ -40,6 +44,10 @@ def detect_current_async_lib():
4044
def detect_current_call_soon_threadsafe():
4145
"""Get the current applicable call_soon_threadsafe function, or None"""
4246

47+
# We could support Pyodide too, but this never gets called on Pyodide anyway (bc USE_THREADED_TIMER). Leaving commented for reference.
48+
# if IS_PYODIDE:
49+
# return sys.modules["asyncio"].get_running_loop().call_soon_threadsafe
50+
4351
# Get asyncgen hook func, return fast when no async loop active
4452
ob = sys.get_asyncgen_hooks()[0]
4553
if ob is None:

tests/test_glfw.py

Whitespace-only changes.

tests/test_scheduling.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ def _test_scheduling_continuous(canvas):
147147

148148
# And a second one after 0.1s, with 10 fps.
149149
canvas.active_sleep(0.1)
150-
assert canvas.draw_count == 2
151-
assert canvas.events_count == 2
150+
assert canvas.draw_count in (2, 3)
151+
assert canvas.events_count in (2, 3)
152152

153153
# And after one second, about 10 more
154154
canvas.draw_count = canvas.events_count = 0

0 commit comments

Comments
 (0)