Results 1 to 4 of 4

Thread: Need concurrency advice with callback function

  1. #1
    Join Date
    Jun 2012
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Need concurrency advice with callback function

    (PyQt 4.8, Python 2.7)
    After reading through the following I'm still left a bit puzzled as to which approach to attempt to utilize for my situation. Any advice would be appreciated.
    Keeping_the_GUI_Responsive

    I have a module, flp.so library to which I have no control over. It takes a function as a constructor argument, and performs a complex/long calculation when invoking the run() method. Here I subclassed QThread, and invoke flp.run inside the thread's run method. In turn the flp module will invoke the callback method, which emits a signal. I catch the signal back in my main thread, and update a model for a QTreeView and update a QProgressBar.
    Qt Code:
    1. def my_runner(QThread):
    2. def __init__(self,callback=None):
    3. self.callback = callback
    4.  
    5. def callback(self):
    6. # f_runner.f_output is array that is appended every iteration of f_runner.run()
    7. emit(SIGNAL('update_main'),self.f_runner.f_output.pop())
    8.  
    9. def run(self):
    10. import flp
    11. self.f_runner = flp.f_runner(callback=self.callback)
    12. self.f_runner.run()
    To copy to clipboard, switch view to plain text mode 

    However, the GUI is not very responsive, as the signals are probably stacking up and the events can't be processed fast enough. I try to avoid invoking QApplication.processEvents(), but tried it here and very bad things happen.

    I have been rethinking this design but have gotten stuck. Eventually I would like the main thread GUI to be responsive while this thread is working and updating the progress bar and table view in the main.

    TYVM for reading.
    Last edited by BreakBad; 29th January 2013 at 18:18.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need concurrency advice with callback function

    I'd have to have some more info to give you any precise hints. Currently I can only advise to batch updates to the model so that the view doesn't have to redraw itself each time you add a row to the model.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2012
    Posts
    25
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Need concurrency advice with callback function

    Thanks for the response!

    The only thing thats happening on the main thread side is connecting the update signal to this slot, where I've just been tinkering with repaint, etc....:

    Qt Code:
    1. self.connect(my_runner,SIGNAL('update_main'),self.update_main)
    2. ....
    3. def update_main(self,data):
    4. self.run_progress_bar.increment()
    5. #self.run_progress_bar.repaint()
    6. row = self.output_model.rowCount()
    7. for col in range(len(data)):
    8. item = QtGui.QStandardItem(data[col])
    9. #self.output_model.beginInsertRows(QtCore.QModelIndex(),row,row)
    10. self.output_model.setItem(row,col,item)
    11. #self.output_model.endInsertRows()
    To copy to clipboard, switch view to plain text mode 

    The runs could be anywhere from a few rows to 1,000 rows, each row taking anywhere from a fraction of a second to half a second to calculate. Eventually my 'customers' would like to also have multiple runs going concurrently. We have 16 processor/core workstations and would like to utilize them. So perhaps using Runnables would be better?

    I agree, that batching the rows of data would be much better. I'll look into creating a consumer thread with a timer *edit: instead of a producer with a signal

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need concurrency advice with callback function

    If your GUI gets frozen then provided that worker threads are running correctly, the problem is with refreshing views thus batching is something that can give you the biggest speedup. I suggest you try it and see for yourself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. using function pointers and callback functions
    By ehnuh in forum General Programming
    Replies: 2
    Last Post: 5th November 2012, 11:00
  2. Qt classes in a callback function
    By Luc4 in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2010, 13:47
  3. Interoperability of C Callback function and Qt
    By Vogel Ubrhar in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2010, 16:02
  4. structs, pointer, static callback function
    By vonCZ in forum General Programming
    Replies: 3
    Last Post: 20th June 2008, 12:53
  5. how to define the callback function in QThread?
    By cxl2253 in forum Qt Programming
    Replies: 6
    Last Post: 30th March 2007, 10:59

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.