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 1 commit
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
Prev Previous commit
Next Next commit
Partially address review
  • Loading branch information
Fidget-Spinner committed Jul 16, 2024
commit 34292311a7ff15434d1d09e02e02d0f2f58028f1
2 changes: 1 addition & 1 deletion Include/cpython/pystate.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ struct _ts {
PyObject *async_gen_firstiter;
PyObject *async_gen_finalizer;

PyObject *asyncio_cached_running_loop; // Strong reference
PyObject *asyncio_running_loop; // Strong reference

PyObject *context;
uint64_t context_ver;
Expand Down
40 changes: 11 additions & 29 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,33 +318,15 @@ get_future_loop(asyncio_state *state, PyObject *fut)
return PyObject_GetAttr(fut, &_Py_ID(_loop));
}


static void
get_running_loop(asyncio_state *state, PyObject **loop)
{
PyThreadState *ts = _PyThreadState_GET();
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)
{
PyThreadState *tstate = _PyThreadState_GET();
Py_XSETREF(tstate->asyncio_cached_running_loop, Py_NewRef(loop));

return 0;
}


static PyObject *
get_event_loop(asyncio_state *state)
{
PyObject *loop;
PyObject *policy;

get_running_loop(state, &loop);
PyThreadState *ts = _PyThreadState_GET();
assert(ts->asyncio_running_loop != NULL);
loop = Py_NewRef(ts->asyncio_running_loop);

if (loop != Py_None) {
return loop;
Expand Down Expand Up @@ -3298,8 +3280,9 @@ _asyncio__get_running_loop_impl(PyObject *module)
/*[clinic end generated code: output=b4390af721411a0a input=0a21627e25a4bd43]*/
{
PyObject *loop;
asyncio_state *state = get_asyncio_state(module);
get_running_loop(state, &loop);
PyThreadState *ts = _PyThreadState_GET();
assert(ts->asyncio_running_loop != NULL);
loop = Py_NewRef(ts->asyncio_running_loop);
return loop;
}

Expand All @@ -3318,10 +3301,8 @@ static PyObject *
_asyncio__set_running_loop(PyObject *module, PyObject *loop)
/*[clinic end generated code: output=ae56bf7a28ca189a input=4c9720233d606604]*/
{
asyncio_state *state = get_asyncio_state(module);
if (set_running_loop(state, loop)) {
return NULL;
}
PyThreadState *tstate = _PyThreadState_GET();
Py_SETREF(tstate->asyncio_running_loop, Py_NewRef(loop));
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -3359,8 +3340,9 @@ _asyncio_get_running_loop_impl(PyObject *module)
/*[clinic end generated code: output=c247b5f9e529530e input=2a3bf02ba39f173d]*/
{
PyObject *loop;
asyncio_state *state = get_asyncio_state(module);
get_running_loop(state, &loop);
PyThreadState *ts = _PyThreadState_GET();
assert(ts->asyncio_running_loop != NULL);
loop = Py_NewRef(ts->asyncio_running_loop);
if (loop == Py_None) {
/* There's no currently running event loop */
PyErr_SetString(
Expand Down
4 changes: 2 additions & 2 deletions Python/pystate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@ init_threadstate(_PyThreadStateImpl *_tstate,
tstate->previous_executor = NULL;
tstate->dict_global_version = 0;

tstate->asyncio_cached_running_loop = Py_None;
tstate->asyncio_running_loop = Py_None;

tstate->delete_later = NULL;

Expand Down Expand Up @@ -1702,7 +1702,7 @@ PyThreadState_Clear(PyThreadState *tstate)

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

Py_CLEAR(tstate->asyncio_cached_running_loop);
Py_SETREF(tstate->asyncio_running_loop, Py_None);

Py_CLEAR(tstate->dict);
Py_CLEAR(tstate->async_exc);
Expand Down
Loading
0