I have a python script that runs multiprocessing.Pool to process a lot of files separately. I usually have a cpu limit of 8. My problem is after running a while I always get "IOError: [Errno 24] Too many open files". Each child process opens a few files for reading only with file.open(). These file handlers are than passed to multiple functions to retrieve data. At the end of the each child process these files are closed with file.close(). I tried the with statement as well but did not fix the issue. Does any one have any idea whats wrong. I googled around but failed to find any answers. I am closing the files and the functions are returning properly so what keep the file handlers around.
My settings are Mac 10.5 with python 2.6
Thanks
Ogan
from custom import func1, func2
# func1 and func2 only seek, read and return values form the file
# however, they do not close the file
import multiprocessing
def Worker(*args):
f1 = open("db1.txt")
f2 = open("db2.txt")
for each in args[1]:
# do many stuff
X = func1(f1)
Y = func2(f2)
f1.close()
f2.close()
return
Data = {1:[2], 2:[3]}
JobP= multiprocessing.Pool(8)
jobP.map_async(Worker, Data.items())
jobP.close()
jobP.join()