websocket, making the option chain, how to close the websocket after pulling data?

healthsecure15
healthsecure15 edited November 2020 in Python client
I am able to pull the data using the following code:

```
def opt_chain():
kws = KiteTicker("xyz", access_tkn)
symbol = 'ITC'
tokens = [1,2,3]

def get_the_quote(ws, ticks):
print(ticks)
ws.close()
kws.close()
def get_option_data(ws, response):
ws.subscribe(tokens)
ws.set_mode(ws.MODE_FULL, tokens)


# Assign callbacks
kws.on_ticks = get_the_quote
kws.on_connect = get_option_data

kws.connect()
```
The problem is if I incorporate this into a script i.e.:

```
option_chain = opt_chain()
# use the data after pulling
x = option_chain[0]
```

The code gets stuck in the opt_chain and does not move onto the `x = option_chain[0]`.

Edit:
If I close using `ws.stop()` then the next lines can be executed, but if I want to run the code again then I get the twisted.internet.error.ReactorNotRestartable error.


TIA
  • rakeshr
    When are you calling option_chain = opt_chain()? Is this after kws.connect().
    If it's after kws.connect(), then nothing will be called, this runs infinite loop on the main ticker thread.
Sign In or Register to comment.