summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTony Solomonik <tony.solomonik@gmail.com>2020-07-08 19:27:31 (GMT)
committerGitHub <noreply@github.com>2020-07-08 19:27:31 (GMT)
commit529f42645d38b6b0075f256814dfb3d220ac7d92 (patch)
tree765b4254eb07900432fa71c28e6cac41e8f5bcb9
parentb26a0db8ea2de3a8a8e4b40e69fc8642c7d7cb68 (diff)
downloadcpython-529f42645d38b6b0075f256814dfb3d220ac7d92.zip
cpython-529f42645d38b6b0075f256814dfb3d220ac7d92.tar.gz
cpython-529f42645d38b6b0075f256814dfb3d220ac7d92.tar.bz2
bpo-41247: asyncio.set_running_loop() cache running loop holder (GH-21401)
The running loop holder cache variable was always set to NULL when calling set_running_loop. Now set_running_loop saves the newly created running loop holder in the cache variable for faster access in get_running_loop. Automerge-Triggered-By: @1st1
-rw-r--r--Misc/NEWS.d/next/Core and Builtins/2020-07-08-22-03-54.bpo-41247.PndYIk.rst2
-rw-r--r--Modules/_asynciomodule.c12
2 files changed, 11 insertions, 3 deletions
diff --git a/Misc/NEWS.d/next/Core and Builtins/2020-07-08-22-03-54.bpo-41247.PndYIk.rst b/Misc/NEWS.d/next/Core and Builtins/2020-07-08-22-03-54.bpo-41247.PndYIk.rst
new file mode 100644
index 0000000..08699b6
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and Builtins/2020-07-08-22-03-54.bpo-41247.PndYIk.rst
@@ -0,0 +1,2 @@
+Always cache the running loop holder when running
+``asyncio.set_running_loop``.
diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c
index b378742..4a1c91e 100644
--- a/Modules/_asynciomodule.c
+++ b/Modules/_asynciomodule.c
@@ -291,10 +291,13 @@ error:
static int
set_running_loop(PyObject *loop)
{
- cached_running_holder = NULL;
- cached_running_holder_tsid = 0;
+ PyObject *ts_dict = NULL;
+
+ PyThreadState *tstate = PyThreadState_Get();
+ if (tstate != NULL) {
+ ts_dict = _PyThreadState_GetDict(tstate); // borrowed
+ }
- PyObject *ts_dict = PyThreadState_GetDict(); // borrowed
if (ts_dict == NULL) {
PyErr_SetString(
PyExc_RuntimeError, "thread-local storage is not available");
@@ -314,6 +317,9 @@ set_running_loop(PyObject *loop)
}
Py_DECREF(rl);
+ cached_running_holder = (PyObject *)rl;
+ cached_running_holder_tsid = PyThreadState_GetID(tstate);
+
return 0;
}