mirror of
https://github.com/dmunozv04/iSponsorBlockTV.git
synced 2025-12-07 12:26:45 +03:00
Merge pull request #250 from dmunozv04/create-web-session-async
This commit is contained in:
@@ -147,6 +147,9 @@ class DeviceListener:
|
|||||||
return_exceptions=True,
|
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):
|
async def finish(devices, web_session, tcp_connector):
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
@@ -160,31 +163,39 @@ def handle_signal(signum, frame):
|
|||||||
raise KeyboardInterrupt()
|
raise KeyboardInterrupt()
|
||||||
|
|
||||||
|
|
||||||
def main(config, debug):
|
async def main_async(config, debug):
|
||||||
loop = asyncio.get_event_loop_policy().get_event_loop()
|
loop = asyncio.get_event_loop_policy().get_event_loop()
|
||||||
tasks = [] # Save the tasks so the interpreter doesn't garbage collect them
|
tasks = [] # Save the tasks so the interpreter doesn't garbage collect them
|
||||||
devices = [] # Save the devices to close them later
|
devices = [] # Save the devices to close them later
|
||||||
if debug:
|
if debug:
|
||||||
loop.set_debug(True)
|
loop.set_debug(True)
|
||||||
asyncio.set_event_loop(loop)
|
tcp_connector = aiohttp.TCPConnector(ttl_dns_cache=300)
|
||||||
tcp_connector = aiohttp.TCPConnector(loop=loop, ttl_dns_cache=300)
|
web_session = aiohttp.ClientSession(connector=tcp_connector)
|
||||||
web_session = aiohttp.ClientSession(loop=loop, connector=tcp_connector)
|
|
||||||
api_helper = api_helpers.ApiHelper(config, web_session)
|
api_helper = api_helpers.ApiHelper(config, web_session)
|
||||||
for i in config.devices:
|
for i in config.devices:
|
||||||
device = DeviceListener(api_helper, config, i, debug, web_session)
|
device = DeviceListener(api_helper, config, i, debug, web_session)
|
||||||
devices.append(device)
|
devices.append(device)
|
||||||
|
await device.initialize_web_session()
|
||||||
tasks.append(loop.create_task(device.loop()))
|
tasks.append(loop.create_task(device.loop()))
|
||||||
tasks.append(loop.create_task(device.refresh_auth_loop()))
|
tasks.append(loop.create_task(device.refresh_auth_loop()))
|
||||||
signal(SIGTERM, handle_signal)
|
signal(SIGTERM, handle_signal)
|
||||||
signal(SIGINT, handle_signal)
|
signal(SIGINT, handle_signal)
|
||||||
try:
|
try:
|
||||||
loop.run_forever()
|
await asyncio.gather(*tasks)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print("Cancelling tasks and exiting...")
|
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:
|
for task in tasks:
|
||||||
task.cancel()
|
task.cancel()
|
||||||
loop.run_until_complete(asyncio.gather(*tasks, return_exceptions=True))
|
await asyncio.gather(*tasks, return_exceptions=True)
|
||||||
finally:
|
finally:
|
||||||
|
await web_session.close()
|
||||||
|
await tcp_connector.close()
|
||||||
loop.close()
|
loop.close()
|
||||||
print("Exited")
|
print("Exited")
|
||||||
|
|
||||||
|
|
||||||
|
def main(config, debug):
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
loop.run_until_complete(main_async(config, debug))
|
||||||
|
loop.close()
|
||||||
|
|||||||
@@ -20,11 +20,6 @@ class YtLoungeApi(pyytlounge.YtLoungeApi):
|
|||||||
web_session: ClientSession = None,
|
web_session: ClientSession = None,
|
||||||
):
|
):
|
||||||
super().__init__("iSponsorBlockTV", logger=logger)
|
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.screen_id = screen_id
|
||||||
self.auth.lounge_id_token = None
|
self.auth.lounge_id_token = None
|
||||||
self.api_helper = api_helper
|
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 def _command(self, command: str, command_parameters: dict = None) -> bool:
|
||||||
async with self._command_mutex:
|
async with self._command_mutex:
|
||||||
return await super()._command(command, command_parameters)
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user