Skip to content

Commit

Permalink
feat: optimize _ensure_debug_daemon (#479)
Browse files Browse the repository at this point in the history
* feat: optimize _ensure_debug_daemon

* Update _debug.pyx

* Update _debug.pxd

* Update _debug.pyx

* Update _debug.pxd

* Update _debug.pyx

* Update _debug.pyx
  • Loading branch information
BobTheBuidler authored Dec 16, 2024
1 parent c34a8e6 commit 79a4afe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
3 changes: 2 additions & 1 deletion a_sync/primitives/_debug.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ cdef class _LoopBoundMixin(_LoggerMixin):

cdef class _DebugDaemonMixin(_LoopBoundMixin):
cdef object _daemon
cdef object _c_ensure_debug_daemon(self, tuple[object] args, dict[str, object] kwargs)
cdef bint _has_daemon
cdef void _c_ensure_debug_daemon(self, tuple[object] args, dict[str, object] kwargs)
cdef object _c_start_debug_daemon(self, tuple[object] args, dict[str, object] kwargs)
32 changes: 20 additions & 12 deletions a_sync/primitives/_debug.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ cdef class _DebugDaemonMixin(_LoopBoundMixin):
See Also:
:class:`_LoggerMixin` for logging capabilities.
"""

def __cinit__(self):
self._has_daemon = False
self._LoopBoundMixin__loop = None

async def _debug_daemon(self, fut: asyncio.Future, fn, *args, **kwargs) -> None:
"""
Expand Down Expand Up @@ -132,7 +136,7 @@ cdef class _DebugDaemonMixin(_LoopBoundMixin):
return ccreate_task_simple(self._debug_daemon(*args, **kwargs))
return loop.create_future()

def _ensure_debug_daemon(self, *args, **kwargs) -> "asyncio.Future[None]":
def _ensure_debug_daemon(self, *args, **kwargs) -> None:
"""
Ensures that the debug daemon task is running.

Expand All @@ -156,17 +160,20 @@ cdef class _DebugDaemonMixin(_LoopBoundMixin):
See Also:
:meth:`_start_debug_daemon` for starting the daemon.
"""
return self._c_ensure_debug_daemon(args, kwargs)
self._c_ensure_debug_daemon(args, kwargs)

cdef object _c_ensure_debug_daemon(self, tuple[object] args, dict[str, object] kwargs):
cdef object daemon = self._daemon
if daemon is None:
if self.check_debug_logs_enabled():
self._daemon = self._c_start_debug_daemon(args, kwargs)
self._daemon.add_done_callback(self._stop_debug_daemon)
else:
self._daemon = self._c_get_loop().create_future()
return self._daemon
cdef void _c_ensure_debug_daemon(self, tuple[object] args, dict[str, object] kwargs):
if self._has_daemon:
return

if self.check_debug_logs_enabled():
daemon = self._c_start_debug_daemon(args, kwargs)
daemon.add_done_callback(self._stop_debug_daemon)
self._daemon = daemon
else:
self._daemon = self._c_get_loop().create_future()

self._has_daemon = True

def _stop_debug_daemon(self, t: Optional[asyncio.Task] = None) -> None:
"""
Expand All @@ -193,5 +200,6 @@ cdef class _DebugDaemonMixin(_LoopBoundMixin):
"""
if t and t != self._daemon:
raise ValueError(f"{t} is not {self._daemon}")
self._daemon.cancel()
t.cancel()
self._daemon = None
self._has_daemon = False

0 comments on commit 79a4afe

Please sign in to comment.