0

I am building an application where I acquire data continuously from a National Instruments device and do some real time processing on it. I am trying to hook the acquisition part to a PyQt GUI so that the user can start, stop and configure the data acquisition and see the data. I have one class for the data acquisition and one class for the GUI. I start the data acquisition process as a thread within the GUI:

# data acquisition class
class intensimetre():    
   #initialize the data acquisition
   def __init__(self,app=None,l1=None,l2=None,l3=None):    

       #...

   #start the data acquisition 
   def demarrer(self,fs=51200.,sensibilite0=12.2,sensibilite1=10.7, rho=1.21, espaceur=0.012, c=343):

       #start the acquisition task
       nidaq.DAQmxStartTask(tache)

    #if i add this line it works       
    #raw_input(u'appuyez sur entree pour continuer')

#gui class
class GUI_acquisition(QtGui.QWidget):
   def __init__(self, app=None):

       super(GUI_acquisition, self).__init__()

       #create the gui
       #...

       #create an instance of the data acquisition class   
       self.intensimetre=
       intensimetre(app=self.app,l1=self.l1,l2=self.l2,l3=self.l3)    

   def demarrer(self):

       #start the data acquisition in a thread
       t = threading.Thread(target=self.intensimetre.demarrer)
       t.start()

The problem is that the application crashes as soon as I start the data acquisition. If I add the raw_input line after starting the acquisition, then it works as it should. However, it doesn't feel like the right way to do it. It seems that once the acquisition is started, the thread returns and the callbacks from the acquisition are not processed properly. Any idea of how to fix this?

Update

I've replaced the raw_input line by an infinite loop polling the state a status attribute:

while self.isRunning:
    sleep(0.1)

I set the isRunning to True attribute when the user presses the start button and to False upon pressing of the stop button. While this works, I don't like polling as I think it can have a negative impact on the performance of my app.

  • change `raw_input()` by `while True: time.sleep(0.01)` – eyllanesc May 29 '18 at 18:56
  • This could cause problems if the user starts and stops the acquisition repeatedly. – Francois Gosselin May 29 '18 at 19:07
  • I understand that but according to the code that you show you do not indicate anything about the acquisition, how the user starts and for the acquisition? Do you create a new thread for it or use the existing one? If you create a new thread, set the thread as a daemon: `t = threading.Thread(target = self.intensimetre.demarrer, daemon = True)`. In general it shows more information. – eyllanesc May 29 '18 at 19:14
  • The user starts and stops the acquisition with buttons on the GUI. I do create a new thread each time the process is started. What does daemon do exactly? – Francois Gosselin May 29 '18 at 19:26
  • https://stackoverflow.com/questions/190010/daemon-threads-explanation – eyllanesc May 29 '18 at 19:29
  • I get "unexpected keyword argument 'daemon' " when I try to this. – Francois Gosselin May 29 '18 at 19:33
  • Another better option is to use a flag, for example create an attribute in the intensimetre class (change it to Intensimetre, check the PEP8), `self.isRunnig = False` in the constructor, then in the method `def demarrer(self):` `self.isRunning = True` `while self.isRunning: time.sleep (0.01)` and when you want to stop it you change `self.isRunning` to False by some method. – eyllanesc May 29 '18 at 19:34
  • Are you using python2? If so, then use: `t = threading.Thread(...)` `t.daemon = True` `t.start()` – eyllanesc May 29 '18 at 19:35
  • yes I use python 2. I'll try this out tomorrow. – Francois Gosselin May 29 '18 at 20:11
  • I tried with the daemon mode, but the problem remains. As for your other suggestion of creating a status attribute and polling it, I would like to avoid this since my app is realtime and I need the best performance possible. – Francois Gosselin May 30 '18 at 13:00
  • I guess I'll have to accept the polling solution since I can't get anything else to work... – Francois Gosselin May 30 '18 at 16:39

0 Answers0