我有两个独立的功能,都使用套接字与服务器通信。一个仅用于保持收集活动状态,而另一个收集收集的数据。
例如
def keep_connection():
while True:
#keeps connection by sending signal to the server
time.sleep(30)
def data_colector():
while True:
out = str(s.recv(auth_file.SocketConnectPort)
var_index = out.find('269=0')
var_1 = (((out[var_index + 13:var_index + 20]).replace('.','')).replace('\\','')).replace('x','')
...
print(var_1,var_2,var_3)
我与 threading 同时运行它们,并且一切正常,
t_1 = threading.Thread(target=data_colector)
t_2 = threading.Thread(target=keep_connection)
t_1.start()
t_2.start()
但是,当我将data_colector()中的 print 更改为 yield 时,它根本不起作用。
def data_colector():
print('confirm thread started')
while True:
#collects data received and converts it into few variables
yield var_1,var_3
没有错误返回,并且似乎运行 data_colector()的 thread 甚至没有凝视。
这是正常行为吗,如何使data_colector()产生 变量?