全球和本地范围混淆,为什么 UnboundLocalError 没有发生?
我正在与WebSocket合作进行实时数据流,并使用2个线程(总计计数mainthread)我的困惑如下:
我从dict live_data开始,用于存储Live live Data的
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()
live_data在function_1中更新function_1
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)
现在我的疑问是,我尚未在其他代码中定义live_data(如果我尝试放置在此处将填写页面)作为全局变量,但是当我在feed_1中更新live_data时,它仍在更新,我可以在任何地方使用它需要的是为什么可能呢?它不应该给unboundlocalerror吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论