Beginner Threaded file reading access

Jesse Noller jnoller at reference-info.com
Tue Mar 23 18:09:24 CET 2004


I'm fairly new to python as a language - and really new to
multithreaded application dev, so please be gentle.

Right now, I have a script that basically looks like:

def foo():

   f = open(file, "r")
   for lines in file.readlines:
        do something
        bar(var, var)
        write stuff to a results file
   file.close
   return

foo()

Now, what the file I am opening contains is a whole bunch of lines
with a filepath in it I need to process with foo() and bar() - each
transaction writes some data to some log files.

I am trying to figure out with how best to approach multithreading
this - I have a definable variable that will determine how many
threads are going, but for now let's choose 5.

With 5 threads - I need each of the 5 threads to read 1 line from the
file (but never the same line) and perform the processing. I never
want the thread count to exceed the 5 threads defined.

Now, I am trying to figure how to do this, and I've been pointed in
the direction of syncronization of the access to the readlines()
function in between threads.

Does anyone have any pointers, or good beginner tutorials on how to
approach this?

-jesse



More information about the Python-list mailing list