X Tutup
The Wayback Machine - https://web.archive.org/web/20221219150834/https://github.com/python/cpython/pull/100345/files
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

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

Open
wants to merge 1 commit into
base: main
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
-------

@@ -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
@@ -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
@@ -0,0 +1,2 @@
Provide C implementation for :func:`asyncio.current_task` for a 4x-6x
speedup.
@@ -3344,6 +3344,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;
}

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


/*********************** PyRunningLoopHolder ********************/


@@ -3599,6 +3637,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

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

X Tutup