Skip to content

Commit cd9f56b

Browse files
authored
Revert "The asyncadapter util now has get_running_loop (#151)" (#155)
1 parent 29ff0c4 commit cd9f56b

File tree

4 files changed

+7
-200
lines changed

4 files changed

+7
-200
lines changed

rendercanvas/_loop.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,12 @@ class BaseLoop:
5252
"""
5353

5454
def __init__(self):
55-
self.__tasks = set() # only used by the async adapter
55+
self.__tasks = set()
5656
self.__canvas_groups = set()
5757
self.__should_stop = 0
5858
self.__state = (
5959
0 # 0: off, 1: ready, 2: detected-active, 3: inter-active, 4: running
6060
)
61-
self._using_adapter = False
6261

6362
def __repr__(self):
6463
full_class_name = f"{self.__class__.__module__}.{self.__class__.__name__}"
@@ -78,20 +77,12 @@ def _register_canvas_group(self, canvas_group):
7877
self.__state = 1
7978
self._rc_init()
8079
self.add_task(self._loop_task, name="loop-task")
81-
self._using_adapter = len(self.__tasks) > 0
8280
self.__canvas_groups.add(canvas_group)
8381

8482
def _unregister_canvas_group(self, canvas_group):
8583
# A CanvasGroup will call this when it selects a different loop.
8684
self.__canvas_groups.discard(canvas_group)
8785

88-
def _get_sniffio_activator(self):
89-
# A CanvasGroup will call this to activate the loop
90-
if self._using_adapter:
91-
return asyncadapter.SniffioActivator(self)
92-
else:
93-
return None
94-
9586
def get_canvases(self) -> list[BaseRenderCanvas]:
9687
"""Get a list of currently active (not-closed) canvases."""
9788
canvases = []
@@ -400,7 +391,7 @@ def _rc_add_task(self, async_func, name):
400391
* The subclass is responsible for cancelling remaining tasks in _rc_stop.
401392
* Return None.
402393
"""
403-
task = asyncadapter.Task(self._rc_call_later, async_func(), name, self)
394+
task = asyncadapter.Task(self._rc_call_later, async_func(), name)
404395
self.__tasks.add(task)
405396
task.add_done_callback(self.__tasks.discard)
406397

rendercanvas/base.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,6 @@ def _draw_frame_and_present(self):
478478
return
479479
self.__is_drawing = True
480480

481-
try:
482-
# sniffio_activator = self._rc_canvas_group?._loop?._get_sniffio_activator()
483-
sniffio_activator = self._rc_canvas_group._loop._get_sniffio_activator()
484-
except AttributeError: # _rc_canvas_group or _loop can be None
485-
sniffio_activator = None
486-
487481
try:
488482
# This method is called from the GUI layer. It can be called from a
489483
# "draw event" that we requested, or as part of a forced draw.
@@ -537,8 +531,6 @@ def _draw_frame_and_present(self):
537531

538532
finally:
539533
self.__is_drawing = False
540-
if sniffio_activator:
541-
sniffio_activator.restore()
542534

543535
# %% Primary canvas management methods
544536

rendercanvas/utils/asyncadapter.py

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
"""
55

66
import logging
7-
import threading
87

9-
from sniffio import thread_local as _sniffio_thread_local
8+
from sniffio import thread_local as sniffio_thread_local
109

1110

1211
logger = logging.getLogger("asyncadapter")
@@ -59,56 +58,14 @@ class CancelledError(BaseException):
5958
pass
6059

6160

62-
class _ThreadLocalWithLoop(threading.local):
63-
loop = None # set default value as a class attr, like sniffio does
64-
65-
66-
_ourloop_thread_local = _ThreadLocalWithLoop()
67-
68-
69-
def get_running_loop() -> object:
70-
"""Return the running event loop. Raise a RuntimeError if there is none.
71-
72-
This function is thread-specific.
73-
"""
74-
# This is inspired by asyncio, and together with sniffio, allows the same
75-
# code to handle asyncio and our adapter for some cases.
76-
loop = _ourloop_thread_local.loop
77-
if loop is None:
78-
raise RuntimeError(f"no running {__name__} loop")
79-
return loop
80-
81-
82-
class SniffioActivator:
83-
def __init__(self, loop):
84-
self.active = True
85-
self.old_loop = _ourloop_thread_local.loop
86-
self.old_name = _sniffio_thread_local.name
87-
_sniffio_thread_local.name = __name__
88-
_ourloop_thread_local.loop = loop
89-
90-
def restore(self):
91-
if self.active:
92-
self.active = False
93-
_sniffio_thread_local.name = self.old_name
94-
_ourloop_thread_local.loop = self.old_loop
95-
96-
def __del__(self):
97-
if self.active:
98-
logger.warning(
99-
"asyncadapter's SniffioActivator.restore() was never called."
100-
)
101-
102-
10361
class Task:
104-
"""Representation of a task, executing a co-routine."""
62+
"""Representation of task, exectuting a co-routine."""
10563

106-
def __init__(self, call_later_func, coro, name, loop):
64+
def __init__(self, call_later_func, coro, name):
10765
self._call_later = call_later_func
10866
self._done_callbacks = []
10967
self.coro = coro
11068
self.name = name
111-
self.loop = loop
11269
self.cancelled = False
11370
self.call_step_later(0)
11471

@@ -138,8 +95,7 @@ def step(self):
13895
result = None
13996
stop = False
14097

141-
sniffio_activator = SniffioActivator(self.loop)
142-
98+
old_name, sniffio_thread_local.name = sniffio_thread_local.name, __name__
14399
try:
144100
if self.cancelled:
145101
stop = True
@@ -156,7 +112,7 @@ def step(self):
156112
logger.error(f"Error in task: {err}")
157113
stop = True
158114
finally:
159-
sniffio_activator.restore()
115+
sniffio_thread_local.name = old_name
160116

161117
# Clean up to help gc
162118
if stop:

tests/test_sniffio.py

Lines changed: 0 additions & 132 deletions
This file was deleted.

0 commit comments

Comments
 (0)