Skip to content
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

Add loop argument to signal router finalize method to finalize without having a running loop #2830

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions sanic/signals.py
Expand Up @@ -277,12 +277,18 @@ def add( # type: ignore

return cast(Signal, signal)

def finalize(self, do_compile: bool = True, do_optimize: bool = False):
def finalize(
self,
do_compile: bool = True,
do_optimize: bool = False,
loop: Optional[asyncio.AbstractEventLoop] = None,
):
"""Finalize the router and compile the routes

Args:
do_compile (bool, optional): Whether to compile the routes. Defaults to `True`.
do_optimize (bool, optional): Whether to optimize the routes. Defaults to `False`.
loop (asyncio.AbstractEventLoop, optional): Event loop override for asyncio.get_running_loop().

Returns:
SignalRouter: The router
Expand All @@ -293,7 +299,7 @@ def finalize(self, do_compile: bool = True, do_optimize: bool = False):
self.add(_blank, "sanic.__signal__.__init__")

try:
self.ctx.loop = asyncio.get_running_loop()
self.ctx.loop = loop or asyncio.get_running_loop()
except RuntimeError:
raise RuntimeError("Cannot finalize signals outside of event loop")

Expand Down