ReactorNotRestartable error while connecting to the websocket

avidhamija
I am trying to run connect to the web socket and I am getting the below error. I tried searching for forums and could not figure out the solution for this. Can someone please help me in figuring the issue out here?
Traceback (most recent call last):

File "<ipython-input-35-ef8cc35a2e1b>", line 1, in <module>
kws.connect()

File "C:\Users\avidh\Anaconda3\lib\site-packages\kiteconnect\ticker.py", line 532, in connect
reactor.run(**opts)

File "C:\Users\avidh\Anaconda3\lib\site-packages\twisted\internet\base.py", line 1260, in run
self.startRunning(installSignalHandlers=installSignalHandlers)

File "C:\Users\avidh\Anaconda3\lib\site-packages\twisted\internet\base.py", line 1240, in startRunning
ReactorBase.startRunning(self)

File "C:\Users\avidh\Anaconda3\lib\site-packages\twisted\internet\base.py", line 748, in startRunning
raise error.ReactorNotRestartable()

ReactorNotRestartable

Below is the code that I have used for the connection.
from kiteconnect import KiteTicker


api_key="t1hpf1q325z2xhqg"
api_secret=""


access_token=""

public_token=""

kws = KiteTicker(api_key,access_token)



def on_ticks(ws,ticks):
print(ticks)
print("\n")

def on_connect(ws,response):
ws.subscribe([177655])
ws.set_mode(ws.MODE_QUOTE,[177655])

kws.on_ticks = on_ticks
kws.on_connect = on_connect


kws.connect()
P.S. I have removed public token and access token values from the above code
  • rakeshr
    @avidhamija
    It seems to be some issue with your Anaconda environment. Websocket code is working fine in our Linux Anaconda environment on Jupyter Notebook.
    Might be this open issue of twisted.
  • avidhamija
    @rakeshr
    Thanks a lot for the response.
    I am very new to Python and Anaconda environment. Is there any way I can sort this out on Windows?
  • rakeshr
    @avidhamija
    If you are not concerned about acute performance computing, you may use the default python setup.
  • avidhamija
    @rakeshr I tried using the default python setup and I am getting the below error first
    >>> def on_ticks(ws,ticks):
    ... print(ticks)
    ... print("\n")
    ...
    >>> def on_connect(ws,response):
    ... ws.subscribe([177655])
    ... ws.set_mode(ws.MODE_QUOTE,[177655])
    ...
    >>>
    >>> kws.on_ticks = on_ticks
    >>> kws.on_connect = on_connect
    >>>
    >>> kws.connect()
    Connection error: 0 - Error parsing instrument tokens.
    Connection error: 1006 - connection was closed uncleanly (None)
    Connection closed: 1006 - connection was closed uncleanly (None)
    Connection error: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
    Connection closed: 1006 - connection was closed uncleanly (WebSocket opening handshake timeout (peer did not finish the opening handshake in time))
    Connection error: 0 - Error parsing instrument tokens.
    Connection error: 0 - Error parsing instrument tokens.
    And, if I try to run kws.connect() again I am getting the same error
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "C:\Users\avidh\AppData\Roaming\Python\Python37\site-packages\kiteconnect\ticker.py", line 532, in connect
    reactor.run(**opts)
    File "C:\Users\avidh\AppData\Roaming\Python\Python37\site-packages\twisted\internet\base.py", line 1282, in run
    self.startRunning(installSignalHandlers=installSignalHandlers)
    File "C:\Users\avidh\AppData\Roaming\Python\Python37\site-packages\twisted\internet\base.py", line 1262, in startRunning
    ReactorBase.startRunning(self)
    File "C:\Users\avidh\AppData\Roaming\Python\Python37\site-packages\twisted\internet\base.py", line 765, in startRunning
    raise error.ReactorNotRestartable()
    twisted.internet.error.ReactorNotRestartable
  • rakeshr
    @avidhamija
    Connection error: 0 - Error parsing instrument tokens.
    Can you re-check, if you are sending instrument token as number not as string?
  • avidhamija
    @rakeshr I checked and the instrument token is being passed as a number (below is the code)
    def on_connect(ws,response):
    ws.subscribe([177655])
    ws.set_mode(ws.MODE_QUOTE,[177655])
  • sujith
    This is not a valid instrument token. You can check the instrument master dump to get a correct instrument token.
  • avidhamija
    @sujith, @rakeshr Sorry for the confusion. This is working completely fine now.
    Thanks a lot, guys.
This discussion has been closed.