wxPython and threads
Iain King
iainking at gmail.com
Thu Jul 19 06:37:56 EDT 2007
On Jul 18, 3:41 am, Benjamin <musiccomposit... at gmail.com> wrote:
> I'm writing a search engine in Python with wxPython as the GUI. I have
> the actual searching preformed on a different thread from Gui thread.
> It sends it's results through a Queue to the results ListCtrl which
> adds a new item. This works fine or small searches, but when the
> results number in the hundreds, the GUI is frozen for the duration of
> the search. I suspect that so many search results are coming in that
> the GUI thread is too busy updating lists to respond to events. I've
> tried buffer the results so there's 20 results before they're sent to
> the GUI thread and buffer them so the results are sent every .1
> seconds. Nothing helps. Any advice would be great.
I do something similar - populating a bunch of comboboxes from data
takes a long time so I run the generator for the comboboxes in another
thread (and let the user use some textboxes in the mean time). A
rough edit toward what you might be doing:
import thread
class Processor(object):
def __init__(self):
self.lock = thread.allocate_lock()
self.alive = False
self.keepalive = False
def start(self):
if not self.alive:
self.alive = True
self.keepalive = True
thread.start_new_thread(self.Process, (None,))
def stop(self):
self.keepalive = False
def process(self, dummy=None):
self.alive = False
class SearchProcessor(Processor):
def __init__(self, entries, lookfor, report):
Processor.__init__(self)
self.entries = entries
self.lookfor = lookfor
self.report = report
def process(self, dummy=None):
for entry in self.entries:
if lookfor in entry:
self.report(entry)
if not self.keepalive:
break
self.report(None)
self.alive = False
results = []
def storeResult(result):
if result != None:
results.append(result)
else:
notifySomeMethod(results)
sp = SearchProcessor(someListOfData, someTextToSearchFor, storeResult)
sp.start()
when the search is done it will call notifySomeMethod with the
results. Meanwhile you could, for example, bind sp.stop() to a cancel
button to stop the thread, or add a counter to the storeResult
function and set a status bar to the total found, or whatever else you
want to do.
Iain
More information about the Python-list
mailing list