1 year ago
#352377
KUNAL CHAVDA
Gloabal and Local scope confusion, Why is UnboundLocalError not happening?
I am working with WebSocket for a live stream of data and using 2 threads (total counting MainThread) my confusion is as follows
I have started with a dict live_data which is used to store live data in it
smart_soket = soket_plugin(feed_token)
live_data = {}
thread_1 = threading.Thread(target=feed_1, name='thread_1',
args=(smart_soket, token_string))
thread_1.start()
here live_data is updated in function feed_1 which is
def feed_1(smart_soket, token_string):
token=token_string
task="mw"
def on_message(ws, ticks):
#print(f"Got a Tick at {dt.datetime.now()}")
#print(ticks)
for stock in ticks:
try:
if stock['tk'] == '26009':
#print('In for Bank Nifty')
live_data[token_to_symbol_map
[stock['tk']]] = {"ltp": stock["ltp"],
"Last_update_time": stock["ltt"]}
else:
live_data[token_to_symbol_map
[stock['tk']]] = {"ltp": stock["ltp"],
"Last_update_time": stock["ltt"],
"Vwap": stock["ap"]}
except:
continue
def on_open(ws):
print("In on_open")
print("on open")
smart_soket.subscribe(task,token)
Now my doubt is I have not defined live_data in further code (which if I try to put here will fill the pages ) as a global variable, but when I update live_data in feed_1 it still is getting updated and I am able to use it wherever needed so why is it possible? shouldn't it have given UnboundLocalError?
python-3.x
websocket
global
local
python-multithreading
0 Answers
Your Answer