Hello there
I am trying to get familiar with the MarketDataStreamer functionality and I need a conceptual clarity around the flow of code execution… Please have a look at the code pulled from the official documentation below:
‘’’
import upstox_client
import time
def main():
** configuration = upstox_client.Configuration()**
** access_token = <ACCESS_TOKEN>**
** configuration.access_token = access_token**
** streamer = upstox_client.MarketDataStreamer(**
** upstox_client.ApiClient(configuration))**
** def on_open():**
** print(“Connected. Subscribing to instrument keys.”)**
** streamer.subscribe(**
** [“NSE_EQ|INE020B01018”, “NSE_EQ|INE467B01029”], “full”)**
** # Handle incoming market data messages**
** def on_message(message):**
** print(message)**
** streamer.on(“open”, on_open)**
** streamer.on(“message”, on_message)**
** streamer.connect()**
** time.sleep(5)**
** print(“Unsubscribing from instrument keys.”)**
** 3. 3. streamer.unsubscribe([“NSE_EQ|INE020B01018”, “NSE_EQ|INE467B01029”])**
if name == “main”:
** main()**
‘’’
My understanding about the order of code execution is as follows:
- Streamer makes a websocket connection using the set configuration
- The connection is established
- On_open function is executed and the streamer subscribes to [“NSE_EQ|INE020B01018”, “NSE_EQ|INE467B01029”] in ‘full’ mode
- Message Iteration 1 is received from the connection
- On_message function is executed
- Message Iteration 2 is received from the connection
- On_message function is executed
- …. And so on….
My question is: where do the following codes fit in the program in the above execution paradigm:
- time.sleep(5)
- print(“Unsubscribing from instrument keys.”)
- streamer.unsubscribe([“NSE_EQ|INE020B01018”, “NSE_EQ|INE467B01029”])
Thanks
Ankush