全球和本地范围混淆,为什么 UnboundLocalError 没有发生?

发布于 2025-01-17 16:02:37 字数 1515 浏览 3 评论 0原文

我正在与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文