Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kristjan/get ready queue #525

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,23 @@ def test_loop_call_later_handle_when_after_fired(self):
self.loop.run_until_complete(fut)
self.assertEqual(handle.when(), when)

def test_get_ready_queue(self):
l1 = len(self.loop.get_ready_queue())
self.loop.call_soon(lambda: None)
l2 = len(self.loop.get_ready_queue())
self.assertEqual(l1, l2 - 1)

def test_ready_handle(self):
def cb(a, b):
return None
args = 1, 2
self.loop.call_soon(cb, *args)
handle = list(self.loop.get_ready_queue())[-1]
self.assertIsInstance(handle, uvloop.loop.Handle)
cb2, args2 = handle.get_callback()
self.assertIs(cb, cb2)
self.assertEqual(args, args2)


class TestBaseAIO(_TestBase, AIOTestCase):
pass
Expand Down
10 changes: 10 additions & 0 deletions uvloop/cbhandles.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ cdef class Handle:
def cancelled(self):
return self._cancelled

def get_callback(self):
"""
Allow access to a python callback and its args.
Return a (callback, args) tuple or None if it is an
internal callback.
"""
if self.cb_type == 1:
return self.arg1, self.arg2
return None


@cython.no_gc_clear
@cython.freelist(DEFAULT_FREELIST_SIZE)
Expand Down
2 changes: 2 additions & 0 deletions uvloop/loop.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ from typing import (
Any,
Awaitable,
Callable,
Deque,
Dict,
Generator,
List,
Expand Down Expand Up @@ -292,3 +293,4 @@ class Loop:
*,
fallback: bool = ...
) -> int: ...
def get_ready_queue(self) -> Deque[asyncio.Handle]: ...
3 changes: 3 additions & 0 deletions uvloop/loop.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3220,6 +3220,9 @@ cdef class Loop:
except Exception as ex:
self.call_soon_threadsafe(future.set_exception, ex)

# Allow external access to the ready queue for advanced scheduling and introspection
def get_ready_queue(self):
return self._ready

# Expose pointer for integration with other C-extensions
def libuv_get_loop_t_ptr(loop):
Expand Down