0

I have a thread class (TransmitThread) written in python which takes few parameters and then starts transmitting to a device(NI USRP). I have a rest flask based webservice,which on rest request by the user creates a new object of TransmitThread and calls the start(). Ideally it should start transmitting and wait for a flag which self.thdRunning which i can make false. I see 2 issues with my implementation 1. I do not see the prints from the Thread class 2. I store the threads in a dictionary as a reference. and when i get a subsequent rest request ,the older thread is in stopped state.

Note: I am not so familiar with Python ,i am trying to learn so any additional inputs will also be helpfull

Any input on how to keep the threads running and printing of the logs will help.

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

Code is @ https://github.com/sagar1805/usrp-server.git

Sagar
  • 30
  • 5
  • Can't see where you assign `thdRunning` default value (`thdRunning = True` or smth). – Fine May 10 '18 at 14:30
  • Yes i have set it true when i create the thread object. – Sagar May 11 '18 at 06:37
  • Haven't reviewed your whole code, but let me guess. You are defining `thdRunning` at a class-level, not instance level. So it's change shuts down _all_ running threads of the `TransmitThread` class [if any instance sets it to False](https://stackoverflow.com/questions/2923579/python-class-attribute), because of how class attributes work. Are you sure this is your desired behaviour? – Fine May 11 '18 at 11:21
  • No.. I have the thdRunning at class level ..i have added the code above..So when i create an instance of the class,the thdRunning must be set to true...The idea is a client can start or stop a transmission to a device.. So on one request he will start the transmission.. on the next request he will ask for stop of transmission.. So i want to have a thread which will start with thdRunning and wait in a while loop until the next request comes and i can make it thdRunning false.. and exit that thread – Sagar May 11 '18 at 12:36
  • @Fian If an instance uses `self.thdRunning = False` it will create a new instance attribute that overrides `thdRunning`. It would change the class attribute only if it used `TransmitThread.thdRunning = False` for the assignment. – Giacomo Alzetta May 11 '18 at 12:47
  • You define "dict" as `dict = ()` (which actually creates a tuple) but use it as a dict `dict[str(usrpIp)] = transmitThread`. I can't see how your code can even work with such a mistake. I think you need a debug session, probably creating firstly minimum working project with flask and threads and then proceed by adding other code. Good luck. – Fine May 11 '18 at 13:06
  • @GiacomoAlzetta I've never faced such a behaviour because using it so is quite implicit and non-obvious, it's better to set different names for class and instance attributes and set default values for instance attributes in init as it more error-prone. But good to know. – Fine May 11 '18 at 13:19

0 Answers0