async def start_websockets(self, websocket_start_time_interval_ms: int = 0) -> None: if len(self.subscription_sets) < 1: raise CryptoXLibException("ERROR: There are no subscriptions to be started.") tasks = [] startup_delay_ms = 0 for id, subscription_set in self.subscription_sets.items(): subscription_set.websocket_mgr = self._get_websocket_mgr(subscription_set.subscriptions, startup_delay_ms, self.ssl_context) tasks.append(async_create_task( subscription_set.websocket_mgr.run()) ) startup_delay_ms += websocket_start_time_interval_ms done, pending = await asyncio.wait(tasks, return_when = asyncio.FIRST_EXCEPTION) for task in done: try: task.result() except Exception as e: LOG.error(f"Unrecoverable exception occurred while processing messages: {e}") LOG.info(f"Remaining websocket managers scheduled for shutdown.") await self.shutdown_websockets() if len(pending) > 0: await asyncio.wait(pending, return_when = asyncio.ALL_COMPLETED) LOG.info("All websocket managers shut down.") raise
async def start_websockets(self) -> None: if len(self.subscription_sets): done, pending = await asyncio.wait( [ asyncio.create_task( self._get_websocket_mgr(subscriptions, self.ssl_context).run()) for subscriptions in self.subscription_sets ], return_when=asyncio.FIRST_EXCEPTION) for task in done: try: task.result() except Exception as e: LOG.exception( f"Unrecoverable exception occurred while processing messages: {e}" ) LOG.info("All websockets scheduled for shutdown") for task in pending: if not task.cancelled(): task.cancel() if len(pending) > 0: await asyncio.wait(pending, return_when=asyncio.ALL_COMPLETED) LOG.info("All websockets closed.") raise else: raise CryptoXLibException( "ERROR: There are no subscriptions to be started.")
async def unsubscribe_subscriptions(self, subscriptions: List[Subscription]) -> None: for subscription in subscriptions: subscription_found = False for id, subscription_set in self.subscription_sets.items(): if subscription_set.find_subscription(subscription) is not None: subscription_found = True await subscription_set.websocket_mgr.unsubscribe(subscriptions) if not subscription_found: raise CryptoXLibException(f"No active subscription {subscription.subscription_id} found.")