[Tutor] Reading in file and processing it in a thread (original) (raw)
Jeff Shannon jeff at ccvcorp.com
Sat Jul 24 00:31:59 CEST 2004
- Previous message: [Tutor] Reading in file and processing it in a thread
- Next message: [Tutor] Reading in file and processing it in a thread
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Vicki Stanfield wrote:
#Test thread code thread2 = thread.startnewthread(self.ThreadTest, ())
Okay, I have set this up as the above code calling this: def ThreadTest(self): print "Got into ThreadTest" inputfile = self.filesel.GetFilename() input = open('C:/'+inputfile, 'r') tokens="" while STOPREAD != wx.TRUE or firstiteration == wx.TRUE: for line in input.readlines(): if len(line)>1: firstiteration == wx.FALSE tokens=line.split("|") self.command = tokens[0][0]+tokens[0][1] self.arguments = tokens[0].lstrip(tokens[0][0]+tokens[0][1]+" ") print self.command print self.arguments self.ProcessCommand(self.command, self.arguments) #wx.GetApp().Yield() else: input.close() But I get a strange error:
Using the same variable in multiple threads (in this case, 'self' if nothing else) is very likely to cause problems. In particular, access to any wx.Python GUI elements across threads is not safe. Make your threaded function entirely independent of the rest of your code (and make it a function rather than a method, unless it's a method of a new object), and then use documented-threadsafe methods to get information back from it. (IIRC, wx.PostEvent() will deliver an event to the main GUI thread in a safe manner; just define a custom event for the main thread to listen for, and have your threaded function post that event with the necessary information attached.)
By the way, is there any reason that you're using the thread module instead of the threading module? The latter is a higher-level, more user-friendly interface built on top of thread, and is usually the recommended module for use.
Jeff Shannon Technician/Programmer Credit International
- Previous message: [Tutor] Reading in file and processing it in a thread
- Next message: [Tutor] Reading in file and processing it in a thread
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]