Implemented sharing the same event loop with AsyncSingleThreadContext#542
Open
Arfey wants to merge 1 commit intodjango:mainfrom
Open
Implemented sharing the same event loop with AsyncSingleThreadContext#542Arfey wants to merge 1 commit intodjango:mainfrom
Arfey wants to merge 1 commit intodjango:mainfrom
Conversation
Arfey
commented
Jan 21, 2026
| ) | ||
| finally: | ||
| loop.close() | ||
|
|
Contributor
Author
There was a problem hiding this comment.
Instead of using asyncio.run, we rely on loop.run_until_complete(asyncio.run closes the event loop), which means we need to perform the additional cleanup that asyncio.run normally handles for us.
Internally, asyncio.run is implemented using asyncio.Runner. Below is a simplified version of Runner, with parts removed that are not relevant to our use case:
class Runner:
def close(self):
try:
_cancel_all_tasks(self._loop)
self._loop.run_until_complete(self._loop.shutdown_asyncgens())
self._loop.run_until_complete(
self._loop.shutdown_default_executor(constants.THREAD_JOIN_TIMEOUT))
finally:
if self._set_event_loop:
events.set_event_loop(None)
loop.close()
def run(self, coro, *, context=None):
self._loop = events.new_event_loop()
events.set_event_loop(self._loop)
task = self._loop.create_task(coro)
return self._loop.run_until_complete(task)I added everything except the set_event_loop logic. In our case, when running code via loop.run_until_complete, asyncio.get_event_loop() already returns the correct loop, so there is no need to set it explicitly. Instead, we reuse the same new_loop_wrap.
ba093e7 to
801bc5a
Compare
10 tasks
b2f1e1f to
da34cca
Compare
da34cca to
912c6ce
Compare
Arfey
commented
Jan 21, 2026
| python -m pip install --upgrade tox tox-py | ||
|
|
||
| - name: Run tox targets for ${{ matrix.python-version }} | ||
| timeout-minutes: 10 |
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

The pull request updates how the
AsyncSingleThreadContextworks inasgiref.syncso that multipleasync_to_synccalls within the same async context share the same asyncio event loop and thread. This improves consistency and correctness when running async code converted to sync repeatedly in one context.