Python: Unable to strem data out of websocket

Hello Team, I am trying to fetch data through websocket and populate on my webpage but when I am trying to do so, Websocket just printing the data and not emitting that and also my terminal is going into hung state even after using Threading, not even able to do control+C to quite. Need to close the terminal that the only option.

Can you please help me with these 2 issues? Code as below:

Copy

@socketio.on('custom_message')
def handle_custom_message(message):
    # Process the incoming data from the Fyers API
    processed_data = test1.run_process_foreground_symbol_data(message)
    print("Emitting data_update event with processed data:", processed_data)
    # Emit the processed data to the connected clients
    emit('data_update', processed_data)

Copy

from fyers_api.Websocket import ws
import threading, time
live_data = {}

def run_process_foreground_symbol_data(message):
    '''This function is used for running the symbolData in foreground
    1. log_path here is configurable this specifies where the output will be stored for you
    2. data_type == symbolData this specfies while using this function you will be able to connect to symbolwebsocket to get the symbolData
    3. run_background = False specifies that the process will be running in foreground'''
    data_type = "symbolData"
    ws_access_token = "FwcFR5cGUiOjEwMCwicG9hX2ZsYWciOiJOIn0.GIJr9U35Z_d7_4UE2_8FLHhShrhzOVaPqW_IzZMv_TM"

    # symbol = ["NSE:SBIN-EQ","NSE:ONGC-EQ"]   ##NSE,BSE sample symbols
    print("run_process_foreground_symbol_data", message, type(message))
    # symbol =[message['callStrikes'], message['putStrikes']]


    # symbol = ["NSE:NIFTY50-INDEX", "NSE:NIFTYBANK-INDEX"]

    symbol =["MCX:CRUDEOIL23JUN6200CE"]
    print(symbol, data_type)
    # fs = ws.FyersSocket(access_token=access_token, run_background=False, log_path="D:\\matx-react-master\\matx-react-master\\backend\\otd_backend\\logs")
    fyersSocket = ws.FyersSocket(access_token=ws_access_token, run_background=False, log_path="D:\\matx-react-master\\matx-react-master\\backend\\otd_backend\\logs")
fyersSocket.websocket_data = custom_message
    threading.Thread(target=subscribe_new_symbol, args=(symbol, fyersSocket, data_type)).start()
    print("Done")

def subscribe_new_symbol(symbol_list, fyersSocket, data_type):
    fyersSocket.subscribe(symbol=symbol_list, data_type=data_type)

def custom_message(msg):
    print("new msg..")
    for symbol_data in msg:
        live_data[symbol_data['symbol']] = {"LTP": symbol_data['ltp']}
        print(live_data)
        time.sleep(1)

Output is as below:

{'MCX:CRUDEOIL23JUN6200CE': {'LTP': 172.0}}

new msg..

{'MCX:CRUDEOIL23JUN6200CE': {'LTP': 171.5}}

new msg..

{'MCX:CRUDEOIL23JUN6200CE': {'LTP': 171.5}}

new msg..

{'MCX:CRUDEOIL23JUN6200CE': {'LTP': 171.5}}


1
5 replies