套接字编程中的python全局变量初始化

问题描述 投票:0回答:1

使用套接字编程库可以创建函数并在回调的每个滴答声中对其进行验证。所以我在顶部声明了一个初始的全局变量,并在勾号中检查了该变量,但出现以下错误:

错误:websocket:来自回调的错误>:分配前引用了本地变量'initial'

似乎在每次调用python全局范围时都未初始化

(zeroconnecty.py)
from kiteconnect import WebSocket

kws = WebSocket("xxxx", "xxxx", "USERNAME")

initial=True
print "initial",initial

#Callback for tick reception.
def on_tick(tick, ws):

    if(initial):
        print "only execute if it called first time callback"
        initial=False
    else:
        print "all times except initial callback"


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

    ws.subscribe([738561])
    ws.set_mode(ws.MODE_FULL, [738561])

#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() 

使用套接字编程库可以创建函数并在回调的每个滴答声中对其进行验证。因此,我在顶部声明了一个初始的全局变量,并在...

python sockets websocket global-variables
1个回答
0
投票

尝试在on_tick()函数中全局引用:

© www.soinside.com 2019 - 2024. All rights reserved.