Skip to content

Commit ef8c620

Browse files
pranavtbhatfacebook-github-bot
authored andcommitted
Optimize asyncio.current_task
Summary: Provide a native implementation of current_task as it is called frequently in the RequestContext scope guards. Reviewed By: czardoz Differential Revision: D39555934 fbshipit-source-id: f6dc3bf896e2509171835d333c6fa454cc6d2a3b
1 parent 9bea80e commit ef8c620

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

Lib/asyncio/tasks.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,6 +975,7 @@ def _unregister_task(task):
975975
from _asyncio import (_register_task, _unregister_task,
976976
_enter_task, _leave_task,
977977
_current_tasks,
978+
current_task,
978979
all_tasks,
979980
Task as CTask,
980981
AsyncLazyValue as _ASYNC_LAZY_VALUE_TYPE,

Modules/_asynciomodule.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7838,6 +7838,40 @@ _asyncio__leave_task_impl(PyObject *module, PyObject *loop, PyObject *task)
78387838
Py_RETURN_NONE;
78397839
}
78407840

7841+
/*[clinic input]
7842+
_asyncio.current_task
7843+
7844+
loop: object = None
7845+
7846+
Return a currently executed task.
7847+
7848+
[clinic start generated code]*/
7849+
7850+
static PyObject *
7851+
_asyncio_current_task_impl(PyObject *module, PyObject *loop)
7852+
/*[clinic end generated code: output=fe15ac331a7f981a input=58910f61a5627112]*/
7853+
{
7854+
PyObject* ret;
7855+
7856+
if (loop == Py_None) {
7857+
loop = get_event_loop();
7858+
}
7859+
7860+
if (loop == NULL) {
7861+
return NULL;
7862+
}
7863+
7864+
ret = PyDict_GetItemWithError(current_tasks, loop);
7865+
if (ret == NULL && PyErr_Occurred()) {
7866+
return NULL;
7867+
} else if (ret == NULL) {
7868+
Py_RETURN_NONE;
7869+
} else {
7870+
Py_INCREF(ret);
7871+
return ret;
7872+
}
7873+
}
7874+
78417875
/*[clinic input]
78427876
_asyncio.all_tasks
78437877
@@ -8525,6 +8559,7 @@ module_init(void)
85258559
PyDoc_STRVAR(module_doc, "Accelerator module for asyncio");
85268560

85278561
static PyMethodDef asyncio_methods[] = {
8562+
_ASYNCIO_CURRENT_TASK_METHODDEF
85288563
_ASYNCIO_GET_EVENT_LOOP_METHODDEF
85298564
_ASYNCIO_GET_RUNNING_LOOP_METHODDEF
85308565
_ASYNCIO__GET_RUNNING_LOOP_METHODDEF

Modules/clinic/_asynciomodule.c.h

Lines changed: 38 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)