Skip to content

Implemented sharing the same event loop with AsyncSingleThreadContext#542

Open
Arfey wants to merge 1 commit intodjango:mainfrom
Arfey:feat/share-the-same-event-loop-for-async-ctx
Open

Implemented sharing the same event loop with AsyncSingleThreadContext#542
Arfey wants to merge 1 commit intodjango:mainfrom
Arfey:feat/share-the-same-event-loop-for-async-ctx

Conversation

@Arfey
Copy link
Contributor

@Arfey Arfey commented Jan 21, 2026

The pull request updates how the AsyncSingleThreadContext works in asgiref.sync so that multiple async_to_sync calls 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.

)
finally:
loop.close()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Arfey Arfey force-pushed the feat/share-the-same-event-loop-for-async-ctx branch 3 times, most recently from ba093e7 to 801bc5a Compare January 21, 2026 16:09
@Arfey Arfey force-pushed the feat/share-the-same-event-loop-for-async-ctx branch 3 times, most recently from b2f1e1f to da34cca Compare January 21, 2026 16:56
@Arfey Arfey force-pushed the feat/share-the-same-event-loop-for-async-ctx branch from da34cca to 912c6ce Compare January 21, 2026 16:59
python -m pip install --upgrade tox tox-py

- name: Run tox targets for ${{ matrix.python-version }}
timeout-minutes: 10
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran into a failing test that caused an endless loop, so I added a timeout to automatically cancel such pipelines.

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant