为什么python对文件句柄数有限制? [英] Why python has limit for count of file handles?
本文介绍了为什么python对文件句柄数有限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了简单的代码进行测试,python脚本中可以打开多少文件:
I writed simple code for test, how much files may be open in python script:
for i in xrange(2000):
fp = open('files/file_%d' % i, 'w')
fp.write(str(i))
fp.close()
fps = []
for x in xrange(2000):
h = open('files/file_%d' % x, 'r')
print h.read()
fps.append(h)
我得到一个例外
IOError: [Errno 24] Too many open files: 'files/file_509'
推荐答案
打开文件的数量受操作系统限制.在 linux 上你可以输入
The number of open files is limited by the operating system. On linux you can type
ulimit -n
看看限制是多少.如果你是root,你可以输入
to see what the limit is. If you are root, you can type
ulimit -n 2048
现在您的程序将运行正常(以 root 身份),因为您已将打开文件的限制提高到 2048 个
now your program will run ok (as root) since you have lifted the limit to 2048 open files
这篇关于为什么python对文件句柄数有限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文