]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128002: add fast path for native tasks in `asyncio.all_tasks` (#129943)
authorKumar Aditya <kumaraditya@python.org>
Mon, 10 Feb 2025 09:34:33 +0000 (15:04 +0530)
committerGitHub <noreply@github.com>
Mon, 10 Feb 2025 09:34:33 +0000 (15:04 +0530)
Modules/_asynciomodule.c

index 4070076d756fcf33564278f0dd0bac9fd67c35f4..449c8a9499261a4d6364bb2d9fa157fb11d2233d 100644 (file)
@@ -3991,6 +3991,19 @@ static inline int
 add_one_task(asyncio_state *state, PyObject *tasks, PyObject *task, PyObject *loop)
 {
     assert(PySet_CheckExact(tasks));
+    if (Task_CheckExact(state, task)) {
+        int pending = 0;
+        Py_BEGIN_CRITICAL_SECTION(task);
+        pending = ((TaskObj *)task)->task_state == STATE_PENDING && ((TaskObj *)task)->task_loop == loop;
+        Py_END_CRITICAL_SECTION();
+        if (pending) {
+            if (PySet_Add(tasks, task) < 0) {
+                return -1;
+            }
+        }
+        return 0;
+    }
+
     PyObject *done = PyObject_CallMethodNoArgs(task, &_Py_ID(done));
     if (done == NULL) {
         return -1;