I had similar problems ending in file descriptors during subprocess.Popen () calls. I used the following script to debug what happens:
import os import stat _fd_types = ( ('REG', stat.S_ISREG), ('FIFO', stat.S_ISFIFO), ('DIR', stat.S_ISDIR), ('CHR', stat.S_ISCHR), ('BLK', stat.S_ISBLK), ('LNK', stat.S_ISLNK), ('SOCK', stat.S_ISSOCK) ) def fd_table_status(): result = [] for fd in range(100): try: s = os.fstat(fd) except: continue for fd_type, func in _fd_types: if func(s.st_mode): break else: fd_type = str(s.st_mode) result.append((fd, fd_type)) return result def fd_table_status_logify(fd_table_result): return ('Open file handles: ' + ', '.join(['{0}: {1}'.format(*i) for i in fd_table_result])) def fd_table_status_str(): return fd_table_status_logify(fd_table_status()) if __name__=='__main__': print fd_table_status_str()
You can import this module and call fd_table_status_str() to register the state of the file descriptor table at different points in your code.
Also, make sure that subprocess.Popen instances are destroyed. Saving references to Popen instances on Windows prevents the GC from starting. And if the instances are saved, the linked channels are not closed. More details here .
mihalop
source share