add trigger before_start events in create_server
This commit is contained in:
parent
f99a723627
commit
d5d1d3b45a
14
sanic/app.py
14
sanic/app.py
|
@ -633,14 +633,28 @@ class Sanic:
|
||||||
warnings.simplefilter('default')
|
warnings.simplefilter('default')
|
||||||
warnings.warn("stop_event will be removed from future versions.",
|
warnings.warn("stop_event will be removed from future versions.",
|
||||||
DeprecationWarning)
|
DeprecationWarning)
|
||||||
|
|
||||||
server_settings = self._helper(
|
server_settings = self._helper(
|
||||||
host=host, port=port, debug=debug, ssl=ssl, sock=sock,
|
host=host, port=port, debug=debug, ssl=ssl, sock=sock,
|
||||||
loop=get_event_loop(), protocol=protocol,
|
loop=get_event_loop(), protocol=protocol,
|
||||||
backlog=backlog, run_async=True,
|
backlog=backlog, run_async=True,
|
||||||
has_log=log_config is not None)
|
has_log=log_config is not None)
|
||||||
|
|
||||||
|
# Trigger before_start events
|
||||||
|
await self.trigger_events(server_settings.get('before_start', []), server_settings.get('loop'))
|
||||||
|
|
||||||
return await serve(**server_settings)
|
return await serve(**server_settings)
|
||||||
|
|
||||||
|
async def trigger_events(self, events, loop):
|
||||||
|
"""Trigger events (functions or async)
|
||||||
|
:param events: one or more sync or async functions to execute
|
||||||
|
:param loop: event loop
|
||||||
|
"""
|
||||||
|
for event in events:
|
||||||
|
result = event(loop)
|
||||||
|
if isawaitable(result):
|
||||||
|
await result
|
||||||
|
|
||||||
async def _run_request_middleware(self, request):
|
async def _run_request_middleware(self, request):
|
||||||
# The if improves speed. I don't know why
|
# The if improves speed. I don't know why
|
||||||
if self.request_middleware:
|
if self.request_middleware:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user