8000 gh-121621: Move asyncio running loop to thread state by Fidget-Spinner · Pull Request #121695 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-121621: Move asyncio running loop to thread state #121695

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

Merged
merged 21 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
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
2 changes: 2 additions & 0 deletions Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ struct _ts {
PyObject *async_gen_firstiter;
PyObject *async_gen_finalizer;

PyObject *asyncio_cached_running_loop; // Strong reference

PyObject *context;
uint64_t context_ver;

Expand Down
1 change: 0 additions & 1 deletion Include/internal/pycore_global_objects_fini_generated.h

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

1 change: 0 additions & 1 deletion Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ struct _Py_global_strings {
STRUCT_FOR_ID(__annotate__)
STRUCT_FOR_ID(__annotations__)
STRUCT_FOR_ID(__args__)
STRUCT_FOR_ID(__asyncio_running_event_loop__)
STRUCT_FOR_ID(__await__)
STRUCT_FOR_ID(__bases__)
STRUCT_FOR_ID(__bool__)
Expand Down
1 change: 0 additions & 1 deletion Include/internal/pycore_runtime_init_generated.h

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

4 changes: 0 additions & 4 deletions Include/internal/pycore_unicodeobject_generated.h

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

99 changes: 11 additions & 88 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,6 @@ typedef struct {
/* Imports from traceback. */
PyObject *trace 8000 back_extract_stack;

PyObject *cached_running_loop; // Borrowed reference
volatile uint64_t cached_running_loop_tsid;

/* Counter for autogenerated Task names */
uint64_t task_name_counter;

Expand Down Expand Up @@ -322,86 +319,20 @@ get_future_loop(asyncio_state *state, PyObject *fut)
}


static int
static void
get_running_loop(asyncio_state *state, PyObject **loop)
{
PyObject *rl;

PyThreadState *ts = _PyThreadState_GET();
uint64_t ts_id = PyThreadState_GetID(ts);
if (state->cached_running_loop_tsid == ts_id &&
state->cached_running_loop != NULL)
{
// Fast path, check the cache.
rl = state->cached_running_loop;
}
else {
PyObject *ts_dict = _PyThreadState_GetDict(ts); // borrowed
if (ts_dict == NULL) {
goto not_found;
}

rl = PyDict_GetItemWithError(
ts_dict, &_Py_ID(__asyncio_running_event_loop__)); // borrowed
if (rl == NULL) {
if (PyErr_Occurred()) {
goto error;
}
else {
goto not_found;
}
}

// TODO GH-121621: This should be moved to PyThreadState
// for easier and quicker access.
state->cached_running_loop = rl;
state->cached_running_loop_tsid = ts_id;
}


if (rl == Py_None) {
goto not_found;
}

*loop = Py_NewRef(rl);
return 0;

not_found:
*loop = NULL;
return 0;

error:
*loop = NULL;
return -1;
assert(ts->asyncio_cached_running_loop != NULL);
*loop = Py_NewRef(ts->asyncio_cached_running_loop);
}


static int
set_running_loop(asyncio_state *state, PyObject *loop)
{
PyObject *ts_dict = NULL;

PyThreadState *tstate = _PyThreadState_GET();
if (tstate != NULL) {
ts_dict = _PyThreadState_GetDict(tstate); // borrowed
}

if (ts_dict == NULL) {
PyErr_SetString(
PyExc_RuntimeError, "thread-local storage is not available");
return -1;
}
if (PyDict_SetItem(
ts_dict, &_Py_ID(__asyncio_running_event_loop__), loop) < 0)
{
return -1;
}


// TODO GH-121621: This should be moved to PyThreadState
// for easier and quicker access.
state->cached_running_loop = loop; // borrowed, kept alive by ts_dict
state->cached_running_loop_tsid = PyThreadState_GetID(tstate);
Py_XSETREF(tstate->asyncio_cached_running_loop, Py_NewRef(loop));

return 0;
}
Expand All @@ -413,10 +344,9 @@ get_event_loop(asyncio_state *state)
PyObject *loop;
PyObject *policy;

if (get_running_loop(state, &loop)) {
return NULL;
}
if (loop != NULL) {
get_running_loop(state, &loop);

if (loop != Py_None) {
return loop;
}

Expand Down Expand Up @@ -3369,13 +3299,7 @@ _asyncio__get_running_loop_impl(PyObject *module)
{
PyObject *loop;
asyncio_state *state = get_asyncio_state(module);
if (get_running_loop(state, &loop)) {
return NULL;
}
if (loop == NULL) {
/* There's no currently running event loop */
Py_RETURN_NONE;
}
get_running_loop(state, &loop);
return loop;
}

Expand Down Expand Up @@ -3436,13 +3360,12 @@ _asyncio_get_running_loop_impl(PyObject *module)
{
PyObject *loop;
asyncio_state *state = get_asyncio_state(module);
if (get_running_loop(state, &loop)) {
return NULL;
}
if (loop == NULL) {
get_running_loop(state, &loop);
if (loop == Py_None) {
/* There's no currently running event loop */
PyErr_SetString(
PyExc_RuntimeError, "no running event loop");
return NULL;
}
return loop;
}
Expand Down
4 changes: 4 additions & 0 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,8 @@ init_threadstate(_PyThreadStateImpl *_tstate,
tstate->previous_executor = NULL;
tstate->dict_global_version = 0;

tstate->asyncio_cached_running_loop = Py_None;

tstate->delete_later = NULL;

llist_init(&_tstate->mem_free_queue);
Expand Down Expand Up @@ -1700,6 +1702,8 @@ PyThreadState_Clear(PyThreadState *tstate)

/* Don't clear tstate->pyframe: it is a borrowed reference */

Py_CLEAR(tstate->asyncio_cached_running_loop);

Py_CLEAR(tstate->dict);
Py_CLEAR(tstate->async_exc);

Expand Down
Loading
0