Skip to content

Commit

Permalink
make async adapter rendercanvas-agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
almarklein committed Dec 6, 2024
1 parent 7843584 commit 46eee9d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import time

from rendercanvas.glfw import RenderCanvas, loop
from rendercanvas.auto import RenderCanvas, loop
from rendercanvas.utils.cube import setup_drawing_sync
from rendercanvas.utils.asyncs import sleep

Expand Down
4 changes: 2 additions & 2 deletions rendercanvas/_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from ._coreutils import logger, log_exception
from .utils.asyncs import sleep
from ._async_adapter import Task as AsyncAdapterTask
from .utils import asyncadapter


HANDLED_SIGNALS = (
Expand Down Expand Up @@ -338,7 +338,7 @@ def _rc_add_task(self, async_func, name):
* The subclass is responsible for cancelling remaining tasks in _rc_stop.
* Return None.
"""
task = AsyncAdapterTask(self, async_func(), name)
task = asyncadapter.Task(self._rc_call_later, async_func(), name)
self.__tasks.add(task)
task.add_done_callback(self.__tasks.discard)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sniffio import thread_local as sniffio_thread_local


logger = logging.getLogger("rendercanvas")
logger = logging.getLogger("asyncadapter")


class Sleeper:
Expand Down Expand Up @@ -62,12 +62,12 @@ class CancelledError(BaseException):
class Task:
"""Representation of task, exectuting a co-routine."""

def __init__(self, loop, coro, name):
self.loop = loop
def __init__(self, call_later_func, coro, name):
self._call_later = call_later_func
self._done_callbacks = []
self.coro = coro
self.name = name
self.cancelled = False
self._done_callbacks = []
self.call_step_later(0)

def add_done_callback(self, callback):
Expand All @@ -81,9 +81,10 @@ def _close(self):
callback(self)
except Exception:
pass
self._done_callbacks.clear()

def call_step_later(self, delay):
self.loop._rc_call_later(delay, self.step)
self._call_later(delay, self.step)

def cancel(self):
self.cancelled = True
Expand Down

0 comments on commit 46eee9d

Please sign in to comment.