[wxPython-users]Multi-thread logging to textctrl
甜瓜
littlesweetmelon at gmail.com
Wed Oct 10 22:44:48 PDT 2007
Hi!
I encounter a race condition in wx and logging library. In my
program, there is a textctrl in mainframe widget. There are two
threads (one is main loop, the other is a working thread) continuously
writing logtext by using logging library.
class OutputWindowStream(object):
def __init__(self, textbox):
# textbox presents the instance of textctrl in the mainframe
self.textbox = textbox
def write(self, s):
# treat the textbox as DebugWindow. logtext will append to it.
self.textbox.AppendText(s)
def flush(self):
pass
In the MainFrame.__init__, a StreamHandler is attached to root logger
in order to redirect logtext into the textbox:
hdler = logging.StreamHandler(OutputWindowStream(self.textbox))
self.log = logging.getLogger()
self.log.addHandler(hdler)
Then, the mainframe start a new working thread:
self.worker = Worker() #
self.worker.log = self.log
self.worker.setDaemon(True)
self.worker.start()
Later, both mainframe and worker start to generate output text by:
self.log.info(...)
However, this program will deadlock randomly. If I remove the
self.log.info in one thread, it runs smoothly. Therefore, the deadlock
is made by logging library while concurrently writing text into a
textctrl widget. But logging library indeed synchronize the access to
OutputWindowStream by internal lock. I'm confused why the race
condition still occurs.
---
ShenLei
More information about the wxpython-users
mailing list