我有一个用python编写的线程类(TransmitThread),它接受很少的参数然后开始传输到设备(NI USRP)。 我有一个基于休息瓶的webservice,用户在休息时请求创建一个TransmitThread的新对象并调用start()。理想情况下,它应该开始传输并等待self.thdRunning的标志,我可以将其伪造。 我看到我的实施有两个问题 1.我没有看到Thread类的打印件 2.我将线程存储在字典中作为参考。当我得到一个后续的休息请求时,旧线程处于停止状态。
注意:我对Python不太熟悉,我正在努力学习,所以任何额外的输入也会有所帮助
有关如何保持线程运行和打印日志的任何输入都会有所帮助。
class TransmitThread(threading.Thread,grc_wxgui.top_block_gui):
// variables are initalised here
thdRunning = True
def __int__(self):
pass
def run(self):
##################################################
# Variables
##################################################
self.samp_rate = samp_rate = 32000
....do some more thing....
print("Transmission started! Will wait to be killed")
while self.thdRunning:
time.sleep(2)
print("Transmission thread stopped!")
def endThreadExecution(self):
self.thdRunning = False;
Flask rest service implementation
@app.route('/txdata/<clienID>/<usrpIp>/<frequency>/<path:fname>',methods=['GET'])
def transmit(clienID,usrpIp,frequency,fname):
global dict
if(not isUsrptransmiting)|shallStartTxn:
if fname.endswith('.iq12'):
decIM = 12
else:
decIM = 48
usrpIp = "addr="+str(usrpIp)
print(usrpIp)
fname = "/"+fname
transmitThread = threading.Thread(target=TransmitThread,args=(fname, str(usrpIp), decIM, frequency))
transmitThread.daemon = True # Daemonize thread
print("Service is Transmitting " + str(clienID) + "this is your username " + str(usrpIp) + " freq " + str(frequency) + " file " + str(fname) +" decIM "+str(decIM))
transmitThread.start()
dict[str(usrpIp)] = transmitThread
print('Service is Transmitting '+ clienID+',this is your username '+usrpIp+ ' freq '+frequency+ ' file '+ fname)
response = {'clientId':clientID}
else:
response = {'clientId':"None"}
return jsonify(response),200