I had an interesting problem with threads and the tempfile module in Python. It seems that something is not being cleared until the threads come out, and I am running against the limit of the open file. (This applies to OS X 10.5.8, Python 2.5.1.)
However, if I replicate what the tempfile module does (not all security checks, it just generates a file descriptor and then using os.fdopen to create the file) I have no problem.
Before writing this as an error with Python, I thought that I would check here, since it is much more likely that I am doing something inaccurate. But if I, then the day, trying to understand this, did not deliver me anywhere.
#!/usr/bin/python import threading import thread import tempfile import os import time import sys NUM_THREADS = 10000 def worker_tempfile(): tempfd, tempfn = tempfile.mkstemp() tempobj = os.fdopen(tempfd, 'wb') tempobj.write('hello, world') tempobj.close() os.remove(tempfn) time.sleep(10) def worker_notempfile(index): tempfn = str(index) + '.txt'
If I started it using the worker_notempfile active line and the worker_tempfile line, it ends.
In another way (using worker_tempfile ) I get the following error:
$ python threadtempfiletest.py Opening thread 0 Opening thread 100 Opening thread 200 Opening thread 300 Exception in thread Thread-301: Traceback (most recent call last): File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py", line 460, in __bootstrap File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/threading.py", line 440, in run File "threadtempfiletest.py", line 17, in worker_tempfile File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/tempfile.py", line 302, in mkstemp File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/tempfile.py", line 236, in _mkstemp_inner OSError: [Errno 24] Too many open files: '/var/folders/4L/4LtD6bCvEoipksvnAcJ2Ok+++Tk/-Tmp-/tmpJ6wjV0'
Any ideas what I'm doing wrong? Is this a bug in Python, or am I with a bone?
UPDATE 2009-12-14: It seems I found an answer, but I don't like it. Since no one could reproduce the problem, I went hunting around our office for cars. He passed everything except my car. I tested on a Mac with the same software versions that I used. I even went in search of the G5 desktop with the EXACT same hardware and software that I had - the same result. Both tests (with tempfile and without tempfile) succeeded in everything.
For kicks, I downloaded Python 2.6.4 and tried it on my desktop, and the same template on my system as Python 2.5.1: tempfile failed, and the notemp file was successful.
This leads me to the conclusion that something was on my Mac, but I definitely can not understand what. Any suggestions are welcome.