Adds some safety around app loop property

This commit is contained in:
Eli Uriegas 2017-02-17 06:54:26 -06:00
parent 36c1122d20
commit 94bb91f2fa

View File

@ -10,7 +10,7 @@ from urllib.parse import urlencode, urlunparse
from sanic.config import Config from sanic.config import Config
from sanic.constants import HTTP_METHODS from sanic.constants import HTTP_METHODS
from sanic.exceptions import ServerError, URLBuildError from sanic.exceptions import ServerError, URLBuildError, SanicException
from sanic.handlers import ErrorHandler from sanic.handlers import ErrorHandler
from sanic.log import log from sanic.log import log
from sanic.response import HTTPResponse from sanic.response import HTTPResponse
@ -50,6 +50,7 @@ class Sanic:
self.debug = None self.debug = None
self.sock = None self.sock = None
self.listeners = defaultdict(list) self.listeners = defaultdict(list)
self.is_running = False
# Register alternative method names # Register alternative method names
self.go_fast = self.run self.go_fast = self.run
@ -57,6 +58,10 @@ class Sanic:
@property @property
def loop(self): def loop(self):
"""Synonymous with asyncio.get_event_loop().""" """Synonymous with asyncio.get_event_loop()."""
if not self.is_running:
raise SanicException(
'Loop can only be retrieved after the app has started '
'running or when not run asynchronously')
return get_event_loop() return get_event_loop()
# -------------------------------------------------------------------- # # -------------------------------------------------------------------- #
@ -459,13 +464,16 @@ class Sanic:
stop_event=stop_event, register_sys_signals=register_sys_signals) stop_event=stop_event, register_sys_signals=register_sys_signals)
try: try:
self.is_running = True
if workers == 1: if workers == 1:
serve(**server_settings) serve(**server_settings)
else: else:
serve_multiple(server_settings, workers, stop_event) serve_multiple(server_settings, workers, stop_event)
except Exception as e: except:
log.exception( log.exception(
'Experienced exception while trying to serve') 'Experienced exception while trying to serve')
finally:
self.is_running = False
log.info("Server Stopped") log.info("Server Stopped")
def stop(self): def stop(self):
@ -477,12 +485,17 @@ class Sanic:
before_stop=None, after_stop=None, ssl=None, before_stop=None, after_stop=None, ssl=None,
sock=None, loop=None, protocol=HttpProtocol, sock=None, loop=None, protocol=HttpProtocol,
backlog=100, stop_event=None): backlog=100, stop_event=None):
"""Asynchronous version of `run`.""" """Asynchronous version of `run`.
NOTE: This does not support multiprocessing and is not the preferred
way to run a Sanic application.
"""
server_settings = self._helper( server_settings = self._helper(
host=host, port=port, debug=debug, before_start=before_start, host=host, port=port, debug=debug, before_start=before_start,
after_start=after_start, before_stop=before_stop, after_start=after_start, before_stop=before_stop,
after_stop=after_stop, ssl=ssl, sock=sock, loop=loop, after_stop=after_stop, ssl=ssl, sock=sock,
protocol=protocol, backlog=backlog, stop_event=stop_event, loop=loop or get_event_loop(), protocol=protocol,
backlog=backlog, stop_event=stop_event,
run_async=True) run_async=True)
return await serve(**server_settings) return await serve(**server_settings)