From bb8289c0951ff9f95764c0e23113ac4ba9c1584a Mon Sep 17 00:00:00 2001 From: Orispik <77781149+Leon-Zh80@users.noreply.github.com> Date: Tue, 25 Nov 2025 23:37:25 +0300 Subject: [PATCH] Update liquidation_order.py --- examples/websocket/liquidation_order.py | 69 ++++++++++++++++++++----- 1 file changed, 57 insertions(+), 12 deletions(-) diff --git a/examples/websocket/liquidation_order.py b/examples/websocket/liquidation_order.py index f7c121a..4d91822 100644 --- a/examples/websocket/liquidation_order.py +++ b/examples/websocket/liquidation_order.py @@ -1,22 +1,67 @@ +#!/usr/bin/env python3 + import time import logging +from typing import Callable, Any +# Assuming 'aster' is a library for a specific exchange/protocol from aster.lib.utils import config_logging from aster.websocket.client.stream import WebsocketClient as Client -config_logging(logging, logging.DEBUG) +# Define the log level for the script. DEBUG is verbose, suitable for development. +LOG_LEVEL = logging.DEBUG + +def setup_logging(): + """Configures the logging system for the application.""" + # NOTE: The original config_logging signature (logging, logging.DEBUG) suggests + # the utility might expect the logging module itself. We maintain the original call + # but use the constant LOG_LEVEL for clarity. + config_logging(logging, LOG_LEVEL) + +def message_handler(message: Any) -> None: + """ + Callback function executed when a message (e.g., liquidation order) is received. + For production, this would contain logic to process the data, not just print it. + """ + # Assuming the message is already deserialized (e.g., dict or JSON object) + print(f"WS MESSAGE RECEIVED: {message}") -def message_handler(message): - print(message) +def main(): + """ + Initializes the WebSocket client, starts the connection, subscribes, + and manages the connection lifecycle. + """ + # Configure logging before client initiation + setup_logging() -my_client = Client() -my_client.start() + # Instantiate the WebSocket client + my_client = Client() -my_client.liquidation_order( - id=13, - callback=message_handler, -) + try: + logging.info("Starting WebSocket client connection...") + # Start the client thread/process + my_client.start() + + # Subscribe to the liquidation order stream + logging.info("Subscribing to liquidation_order stream (ID: 13)...") + my_client.liquidation_order( + id=13, + callback=message_handler, + ) -time.sleep(2) + # HACK: Use sleep to keep the main thread alive while the WS thread runs. + # In a real application, consider using `asyncio` or a proper signaling + # mechanism (like threading.Event) to wait indefinitely or until a condition is met. + logging.info("Connection running. Waiting for 5 seconds to receive messages...") + time.sleep(5) + + except Exception as e: + logging.error(f"An unexpected error occurred: {e}", exc_info=True) + + finally: + # Gracefully stop the client connection + logging.info("Stopping WebSocket client connection.") + my_client.stop() + logging.info("Client stopped. Program exiting.") -logging.debug("closing ws connection") -my_client.stop() +if __name__ == '__main__': + main()