Multithreading help in python for websocket

mattsr71
Hi,

I am trying to make websokets separate so that there is no interruption by my main algorithm.I tried to test with a small code but its not working. I have pasted my code please help me.

My requirement is :- websockets should be always streaming the data and store in a queue, i want to read that queue in another parallel thread and process the data.Please guide me
from kiteconnect import WebSocket
from kiteconnect import KiteConnect
from datetime import datetime
import sys
import time
import threading



api_key = "xx"
user_id ="xx"
api_secret="xxx"
kite =KiteConnect(api_key=api_key)
subcribe_scrips=[53345287,53387015]


kite.set_access_token("xxx")
public_token ="xxx"
# Initialise.
kws = WebSocket(api_key,public_token,user_id)

cash = 0

def margin_cal():
global cash
dat = kite.margins("equity")
avail = dat["available"]
cash = avail["cash"]
print("cash is %d" % cash)
#margin_cal()

def on_tick(tick, ws):
print(tick)

# Callback for successful connection.
def on_connect(ws):
ws.subscribe(subcribe_scrips)

# Set RELIANCE to tick in `full` mode.
ws.set_mode(ws.MODE_QUOTE,subcribe_scrips)
#ws.set_mode(ws.MODE_FULL, [10323202,10323458])
#ws.set_mode(ws.MODE_QUOTE, [11796738])

# Assign the callbacks.
kws.on_tick = on_tick
kws.on_connect = on_connect

# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
#kws.connect()
def timer():
for i in range(0,20):
time.sleep(2)
print("im awake")


t1 = threading.Thread(target=kws.connect()) #Thread 1
t2 = threading.Thread(target=timer()) #Thread 2
t2.start()
t1.start()
t1.join()
t2.join()
#time.sleep(20)
  • naz
    kws.connect(threaded=True) needs to have its argument threaded set to True for your requirement, I beilieve.
  • sauravkedia
    sauravkedia edited October 2017
    In these two line, just remove the brackets after function name and report if it works or not.
    So, in
    t1 = threading.Thread(target=kws.connect()) #Thread 1
    t2 = threading.Thread(target=timer()) #Thread 2

    make it:
    t1 = threading.Thread(target=kws.connect) #Thread 1
    t2 = threading.Thread(target=timer) #Thread 2

    If that's solved, then we move to other points.

    Your Thread2 doesnt seem to be doing anything. Seemingly, you want to check if messages are coming or not. So, in your Thread1, you are printing the message and if they stop coming, you know ticks are not coming. Thread2 is similar mechanism, you got to watch for the message. If you want to watch, then why not watch Thread1.

    In addition, Thread2 will not serve your purpose anyways. It will merely tell you Thread2 is running without disclosing anything about Thread1! In fact, Thread2 may die while Thread1 may continue to run and vice versa.

    Solution:

    Part1: Similar to on_tick, Websocket have a on_message method, which will print a garbage line every one second just to tell you that feed is alive.

    So you can run both on same thread. Mostly, in implementation you will print on_message and do whatever you need to do on on_tick method.

    But even thats not necessary. You may print a small word like 'ok' everytime you recieve a tick
    in on_tick method itself. Data keeps coming every 2 seconds or so, thats fast enough.

    Part2: Your worry is that you need to ensure Thread1 keeps running all the time. For that simple way is to run a Thread1 on main program which is infinite loop. Thread1 calls Thread2 which has the websocket methods. So, when Thread1 runs, it will call Thread2, run websocket and wait. If Thread2 dies, control will passback to Thread1 and it will spawn Thread2 again and run websocket. I use it everywhere where I want to ensure that a certain method has to keep running and it works like a charm. Hope, I have not made it too confusing.
Sign In or Register to comment.