global event loop

This commit is contained in:
Devaev Maxim
2019-04-09 08:04:36 +03:00
parent a6028c46a4
commit 60849efa72
7 changed files with 41 additions and 63 deletions

View File

@@ -217,8 +217,6 @@ class Server: # pylint: disable=too-many-instance-attributes
atx: Atx,
msd: MassStorageDevice,
streamer: Streamer,
loop: asyncio.AbstractEventLoop,
) -> None:
self._auth_manager = auth_manager
@@ -230,8 +228,6 @@ class Server: # pylint: disable=too-many-instance-attributes
self.__msd = msd
self.__streamer = streamer
self.__loop = loop
self.__heartbeat: Optional[float] = None # Assigned in run() for consistance
self.__sockets: Set[aiohttp.web.WebSocketResponse] = set()
self.__sockets_lock = asyncio.Lock()
@@ -257,23 +253,6 @@ class Server: # pylint: disable=too-many-instance-attributes
setproctitle.setproctitle("[main] " + setproctitle.getproctitle())
self.__heartbeat = heartbeat
app = aiohttp.web.Application(loop=self.__loop)
app.on_shutdown.append(self.__on_shutdown)
app.on_cleanup.append(self.__on_cleanup)
for name in dir(self):
method = getattr(self, name)
if inspect.ismethod(method):
if getattr(method, _ATTR_SYSTEM_TASK, False):
self.__system_tasks.append(self.__loop.create_task(method()))
elif getattr(method, _ATTR_EXPOSED, False):
# router = app.router
router = getattr(app, "router") # FIXME: Dirty hack to avoid pylint crash
router.add_route(
getattr(method, _ATTR_EXPOSED_METHOD),
getattr(method, _ATTR_EXPOSED_PATH),
method,
)
assert port or unix_path
if unix_path:
@@ -289,7 +268,7 @@ class Server: # pylint: disable=too-many-instance-attributes
socket_kwargs = {"host": host, "port": port}
aiohttp.web.run_app(
app=app,
app=self.__make_app(),
access_log_format=access_log_format,
print=self.__run_app_print,
**socket_kwargs,
@@ -513,7 +492,27 @@ class Server: # pylint: disable=too-many-instance-attributes
self.__reset_streamer = True
return _json()
# =====
# ===== SYSTEM STUFF
async def __make_app(self) -> aiohttp.web.Application:
app = aiohttp.web.Application()
app.on_shutdown.append(self.__on_shutdown)
app.on_cleanup.append(self.__on_cleanup)
for name in dir(self):
method = getattr(self, name)
if inspect.ismethod(method):
if getattr(method, _ATTR_SYSTEM_TASK, False):
self.__system_tasks.append(asyncio.create_task(method()))
elif getattr(method, _ATTR_EXPOSED, False):
# router = app.router
router = getattr(app, "router") # FIXME: Dirty hack to avoid pylint crash
router.add_route(
getattr(method, _ATTR_EXPOSED_METHOD),
getattr(method, _ATTR_EXPOSED_PATH),
method,
)
return app
def __run_app_print(self, text: str) -> None:
logger = get_logger()