diff --git a/src/iSponsorBlockTV/main.py b/src/iSponsorBlockTV/main.py index dd67b45..c105f14 100644 --- a/src/iSponsorBlockTV/main.py +++ b/src/iSponsorBlockTV/main.py @@ -147,6 +147,9 @@ class DeviceListener: return_exceptions=True, ) + async def initialize_web_session(self): + await self.lounge_controller.change_web_session(self.web_session) + async def finish(devices, web_session, tcp_connector): await asyncio.gather( @@ -160,31 +163,39 @@ def handle_signal(signum, frame): raise KeyboardInterrupt() -def main(config, debug): +async def main_async(config, debug): loop = asyncio.get_event_loop_policy().get_event_loop() tasks = [] # Save the tasks so the interpreter doesn't garbage collect them devices = [] # Save the devices to close them later if debug: loop.set_debug(True) - asyncio.set_event_loop(loop) - tcp_connector = aiohttp.TCPConnector(loop=loop, ttl_dns_cache=300) - web_session = aiohttp.ClientSession(loop=loop, connector=tcp_connector) + tcp_connector = aiohttp.TCPConnector(ttl_dns_cache=300) + web_session = aiohttp.ClientSession(connector=tcp_connector) api_helper = api_helpers.ApiHelper(config, web_session) for i in config.devices: device = DeviceListener(api_helper, config, i, debug, web_session) devices.append(device) + await device.initialize_web_session() tasks.append(loop.create_task(device.loop())) tasks.append(loop.create_task(device.refresh_auth_loop())) signal(SIGTERM, handle_signal) signal(SIGINT, handle_signal) try: - loop.run_forever() + await asyncio.gather(*tasks) except KeyboardInterrupt: print("Cancelling tasks and exiting...") - loop.run_until_complete(finish(devices, web_session, tcp_connector)) + await finish(devices, web_session, tcp_connector) for task in tasks: task.cancel() - loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True)) + await asyncio.gather(*tasks, return_exceptions=True) finally: + await web_session.close() + await tcp_connector.close() loop.close() print("Exited") + + +def main(config, debug): + loop = asyncio.get_event_loop() + loop.run_until_complete(main_async(config, debug)) + loop.close() \ No newline at end of file diff --git a/src/iSponsorBlockTV/ytlounge.py b/src/iSponsorBlockTV/ytlounge.py index 1b73690..52c0535 100644 --- a/src/iSponsorBlockTV/ytlounge.py +++ b/src/iSponsorBlockTV/ytlounge.py @@ -20,11 +20,6 @@ class YtLoungeApi(pyytlounge.YtLoungeApi): web_session: ClientSession = None, ): super().__init__("iSponsorBlockTV", logger=logger) - if web_session is not None: - loop = asyncio.get_event_loop() - loop.run_until_complete(self.session.close()) - loop.run_until_complete(self.conn.close()) - self.session = web_session # And use the one we passed self.auth.screen_id = screen_id self.auth.lounge_id_token = None self.api_helper = api_helper @@ -195,3 +190,10 @@ class YtLoungeApi(pyytlounge.YtLoungeApi): async def _command(self, command: str, command_parameters: dict = None) -> bool: async with self._command_mutex: return await super()._command(command, command_parameters) + + async def change_web_session(self, web_session: ClientSession): + if self.session is not None: + await self.session.close() + if self.conn is not None: + await self.conn.close() + self.session = web_session