[Tutor] Reading in file and processing it in a thread
Vicki Stanfield
vicki at thepenguin.org
Fri Jul 23 17:21:58 CEST 2004
I am doing threading for the first time in Python. I need to read in and process a file recursively if a checkbox on my gui is checked (wxPython) and stop at the end of the file if it is not. I used to have an EVT handler which called the following code (I know it ain't pretty!):
------------
def ReadInFile(self):
inputfile = self.filesel.GetFilename()
input = open('C:/'+inputfile, 'r')
tokens=""
first_iteration = wx.TRUE
#While not STOP_READ, iterate through lines in file
while STOP_READ != wx.TRUE or first_iteration == wx.TRUE:
for line in input.readlines():
if len(line)>1:
first_iteration == 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()
--------------------
Now I want to create a thread in which to run the looping so that the gui is (relatively) independent and a button on it can be used to toggle the STOP_READ flag. I think I need to yank most of the above code out and run it as a thread. I added this and can run the thread with it:
#Test thread code
thread2 = thread.start_new_thread(self.ThreadTest, ())
so I can make the new thread start. Do I want the open and close of the file in the new thread or should input be passed to the thread from outside? If it should be passed in, how do I convert input to a tuple? I have a lot of confusion on threads at this point, so I apoligize if this simple threading exercise should be obvious to me. The examples that I have seen via google haven't been close enough to help me.
Thanks,
vicki
"A pessimist sees the difficulty in every opportunity; an optimist sees the opportunity in every difficulty."
-- Winston Churchill
More information about the Tutor
mailing list