Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-100344: Provide C implementation for asyncio.current_task#100345

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
11 commits
Select commitHold shift + click to select a range
76cbbc6
gh-100344: Provide C implementation for asyncio.current_task
itamaroSep 18, 2022
c7af32b
Merge branch 'main' into asyncio-current-task-native-impl
itamaroDec 20, 2022
5f33beb
Fix refleak in C impl of current_task
itamaroDec 20, 2022
37072a1
Optimize also the common case of the Python implementation of asyncio…
itamaroDec 20, 2022
5a8a8fd
Merge branch 'main' into asyncio-current-task-native-impl
itamaroDec 20, 2022
8f0f80a
Catch more specific KeyError exception
itamaroDec 21, 2022
04bf985
Merge branch 'main' into asyncio-current-task-native-impl
itamaroDec 21, 2022
637dfa7
Merge branch 'main' into asyncio-current-task-native-impl
itamaroDec 21, 2022
c96db2c
Adjust CurrentLoopTests to cover both C and Python implementations of…
itamaroDec 21, 2022
bdeb553
Merge branch 'main' into asyncio-current-task-native-impl
itamaroDec 22, 2022
391db59
Revert optimizations in Python implementation of asyncio.current_task
itamaroDec 22, 2022
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
3 changes: 3 additions & 0 deletionsDoc/whatsnew/3.12.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -225,6 +225,9 @@ asyncio
a custom event loop factory.
(Contributed by Kumar Aditya in :gh:`99388`.)

* Add C implementation of :func:`asyncio.current_task` for 4x-6x speedup.
(Contributed by Itamar Ostricher and Pranav Thulasiram Bhat in :gh:`100344`.)

inspect
-------

Expand Down
5 changes: 4 additions & 1 deletionLib/asyncio/tasks.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -964,6 +964,7 @@ def _unregister_task(task):
_all_tasks.discard(task)


_py_current_task = current_task
_py_register_task = _register_task
_py_unregister_task = _unregister_task
_py_enter_task = _enter_task
Expand All@@ -973,10 +974,12 @@ def _unregister_task(task):
try:
from _asyncio import (_register_task, _unregister_task,
_enter_task, _leave_task,
_all_tasks, _current_tasks)
_all_tasks, _current_tasks,
current_task)
except ImportError:
pass
else:
_c_current_task = current_task
_c_register_task = _register_task
_c_unregister_task = _unregister_task
_c_enter_task = _enter_task
Expand Down
21 changes: 14 additions & 7 deletionsLib/test/test_asyncio/test_tasks.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2804,6 +2804,7 @@ class CIntrospectionTests(test_utils.TestCase, BaseTaskIntrospectionTests):


class BaseCurrentLoopTests:
current_task = None

def setUp(self):
super().setUp()
Expand All@@ -2814,33 +2815,39 @@ def new_task(self, coro):
raise NotImplementedError

def test_current_task_no_running_loop(self):
self.assertIsNone(asyncio.current_task(loop=self.loop))
self.assertIsNone(self.current_task(loop=self.loop))

def test_current_task_no_running_loop_implicit(self):
with self.assertRaisesRegex(RuntimeError, 'no running event loop'):
asyncio.current_task()
self.current_task()

def test_current_task_with_implicit_loop(self):
async def coro():
self.assertIs(asyncio.current_task(loop=self.loop), task)
self.assertIs(self.current_task(loop=self.loop), task)

self.assertIs(asyncio.current_task(None), task)
self.assertIs(asyncio.current_task(), task)
self.assertIs(self.current_task(None), task)
self.assertIs(self.current_task(), task)

task = self.new_task(coro())
self.loop.run_until_complete(task)
self.assertIsNone(asyncio.current_task(loop=self.loop))
self.assertIsNone(self.current_task(loop=self.loop))


class PyCurrentLoopTests(BaseCurrentLoopTests, test_utils.TestCase):
current_task = staticmethod(tasks._py_current_task)

def new_task(self, coro):
return tasks._PyTask(coro, loop=self.loop)


@unittest.skipUnless(hasattr(tasks, '_CTask'),
@unittest.skipUnless(hasattr(tasks, '_CTask') and
hasattr(tasks, '_c_current_task'),
'requires the C _asyncio module')
class CCurrentLoopTests(BaseCurrentLoopTests, test_utils.TestCase):
if hasattr(tasks, '_c_current_task'):
current_task = staticmethod(tasks._c_current_task)
else:
current_task = None

def new_task(self, coro):
return getattr(tasks, '_CTask')(coro, loop=self.loop)
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
Provide C implementation for :func:`asyncio.current_task` for a 4x-6x
speedup.
39 changes: 39 additions & 0 deletionsModules/_asynciomodule.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3314,6 +3314,44 @@ _asyncio__leave_task_impl(PyObject *module, PyObject *loop, PyObject *task)
}


/*[clinic input]
_asyncio.current_task

loop: object = None

Return a currently executed task.

[clinic start generated code]*/

static PyObject *
_asyncio_current_task_impl(PyObject *module, PyObject *loop)
/*[clinic end generated code: output=fe15ac331a7f981a input=58910f61a5627112]*/
{
PyObject *ret;
asyncio_state *state = get_asyncio_state(module);

if (loop == Py_None) {
loop = _asyncio_get_running_loop_impl(module);
if (loop == NULL) {
return NULL;
}
} else {
Py_INCREF(loop);
}

ret = PyDict_GetItemWithError(state->current_tasks, loop);
Py_DECREF(loop);
if (ret == NULL && PyErr_Occurred()) {
return NULL;
}
else if (ret == NULL) {
Py_RETURN_NONE;
}
Py_INCREF(ret);
return ret;
}


/*********************** Module **************************/


Expand DownExpand Up@@ -3494,6 +3532,7 @@ module_init(asyncio_state *state)
PyDoc_STRVAR(module_doc, "Accelerator module for asyncio");

static PyMethodDef asyncio_methods[] = {
_ASYNCIO_CURRENT_TASK_METHODDEF
_ASYNCIO_GET_EVENT_LOOP_METHODDEF
_ASYNCIO_GET_RUNNING_LOOP_METHODDEF
_ASYNCIO__GET_RUNNING_LOOP_METHODDEF
Expand Down
62 changes: 61 additions & 1 deletionModules/clinic/_asynciomodule.c.h
View file
Open in desktop

Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.


[8]ページ先頭

©2009-2025 Movatter.jp