Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Patch event loop policy to always return a patched loop
Browse files Browse the repository at this point in the history
  • Loading branch information
erdewit committed Jul 27, 2023
1 parent 6baf3be commit 9d46de9
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions nest_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
def apply(loop=None):
"""Patch asyncio to make its event loop reentrant."""
_patch_asyncio()
_patch_policy()
_patch_task()
_patch_tornado()

Expand Down Expand Up @@ -63,6 +64,20 @@ def _get_event_loop(stacklevel=3):
asyncio._nest_patched = True


def _patch_policy():
"""Patch the policy to always return a patched loop."""

def get_event_loop(self):
if self._local._loop is None:
loop = self.new_event_loop()
_patch_loop(loop)
self.set_event_loop(loop)
return self._local._loop

policy = events.get_event_loop_policy()
policy.__class__.get_event_loop = get_event_loop


def _patch_loop(loop):
"""Patch loop to make it reentrant."""

Expand Down

0 comments on commit 9d46de9

Please sign in to comment.