Reputation: 4375
I'm attempting to thread some gui code to get rid of the lag in a combo box. The option the user selects does a bit of web scraping in the background, and depending on which option they select, the lag can sometimes be substantial as the program seeks the data it needs.
For the docs, it seems that to start a thread, I only need call threading.Thread
with the appropriate arguments. So, my attempt of this in code is as follows:
def EvtComboBox(self, event):
self.statusBox.AppendText('Fetching ' + event.GetString() + ' data.\n')
loadingThread = threading.Thread(target=self.doStuff(event), name='i_am_a_thread')
loadingThread.start()
threading.Thread(target=self.checker(loadingThread)).start()
def doStuff(self, event):
self.statusBox.AppendText(event.GetString() + ':\n')
self.assets = self.scraper.loadConferencePage(event.GetString())
self.statusBox.AppendText(str(self.scraper.getNumOfNewHandouts(self.assets)) + ' new handout(s) in Asset Manager\n' +
'-------------------------\n\n')
def checker(self, loadingThread):
while threading.Thread.isAlive(loadingThread):
print True
The behavior I'm expecting is that the loadingthread
starts, and begins fetching the data it needs from the web. While it is doing that, I thought the second thread I create, would monitor that first thread, and my final assumption was that since those two threads are running "concurrently", the EvtComboBox method would finish, and the combobox in the gui would close.
However, none of that happens. The EvtComboBox
method fires, and then the code seems to run completely sequentially. If I stick a print statement below the two threads (just to see when it gets executed,
def EvtComboBox(self, event):
self.statusBox.AppendText('Fetching ' + event.GetString() + ' data.\n')
loadingThread = threading.Thread(target=self.doStuff(event), name='i_am_a_thread')
loadingThread.start()
threading.Thread(target=self.checker(loadingThread)).start()
print 'EvtComboBox method finished'
It only gets executed after the code the threads call gets run... so.. I'm unsure why it is behaving in this manner.
Why isn't it executing concurrently?
Upvotes: 0
Views: 252
Reputation: 438
In the target
part of threading.Thread
you should not be calling the function, instead you give it the function object and arguments; such as threading.Thread(target=self.checker args=loadingThread).start()
Upvotes: 4