查看您的previous question(您在评论中链接了),问题是文件描述符用完了.
File descriptors are small integers corresponding to a file that has been opened by the current process. For example, standard input is usually file descriptor 0, standard output is 1, and standard error is 2. Further files opened by a process will then be assigned 3, 4, 5, and so forth. The name “file descriptor” is slightly deceptive; on Unix platforms, sockets and pipes are also referenced by file descriptors.
现在我正在猜测,但可能是您正在执行以下操作:
class Wroker(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
my_file = open('example.txt')
# operations
my_file.close() # without this line!
您需要关闭文件!
您可能正在启动多个线程,并且每个线程都在打开但没有关闭文件,这样一段时间后,您就没有更多的“小整数”要分配给打开新文件了.
还要注意,在#operations部分中可能发生任何事情,如果引发异常,除非包裹在try / finally语句中,否则文件不会关闭.
有一种更好的文件处理方式:with statement:
with open('example.txt') as my_file:
# bunch of operations with the file
# other operations for which you don't need the file