我有一个巨大的文件,我写入大约450个文件.我收到错误了too many files open.我在网上搜索并找到了一些解决方案,但它没有帮助.
import resource
resource.setrlimit(resource.RLIMIT_NOFILE, (1000,-1))
>>> len(pureResponseNames) #Filenames
434
>>> resource.getrlimit(resource.RLIMIT_NOFILE)
(1000, 9223372036854775807)
>>> output_files = [open(os.path.join(outpathDirTest, fname) + ".txt", "w") for fname in pureResponseNames]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 24] Too many open files: 'icd9_737.txt'
>>>
Run Code Online (Sandbox Code Playgroud)
我也ulimit从命令行更改如下:
$ ulimit -n 1200
$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
max locked memory (kbytes, -l) unlimited
max memory size (kbytes, -m) unlimited
open files (-n) 1200
pipe size (512 bytes, -p) 1
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 709
virtual memory (kbytes, -v) unlimited
$
Run Code Online (Sandbox Code Playgroud)
我仍然得到同样的错误.PS:我也重新启动了我的系统并运行程序但没有成功.
dev*_*ail 16
我将 ulimit 更改为4096from1024并且它有效。以下是程序:
使用以下命令检查描述符数量限制:
ulimit -n
Run Code Online (Sandbox Code Playgroud)
对我来说是的1024,我将其更新为4096并且有效。
ulimit -n 4096
Run Code Online (Sandbox Code Playgroud)
pub*_*her 15
"太多打开文件"错误总是很棘手 - 您不仅需要扭曲ulimit,而且还必须检查系统范围限制和OSX特定.这篇SO帖子提供了有关OSX中打开文件的更多信息.(扰流警报:默认值为256).
但是,通常很容易限制必须同时打开的文件数.如果我们看一下Stefan Bollman的例子,我们可以很容易地将其改为:
pureResponseNames = ['f'+str(i) for i in range(434)]
outpathDirTest="testCase/"
output_files = [os.path.join(outpathDirTest, fname) + ".txt" for fname in pureResponseNames]
for filename in range(output_files):
with open(filename, 'w') as f:
f.write('This is a test of file nr.'+str(i))
Run Code Online (Sandbox Code Playgroud)
如果由于某些原因而无法关闭文件(例如,您正在使用3rd party模块),则可以考虑基于hard最大限制而不是预定义的硬编码限制ValueError进行设置(如果尝试设置则会抛出该错误hard+1):
import resource
soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard))
Run Code Online (Sandbox Code Playgroud)
而且我想说清楚,即使您手动删除在python进程仍运行时创建的文件,以后仍会引发此类错误。
Ste*_*ann -2
一个最小的工作示例会很好。我在 mac 10.6.8 上使用 Python 3.3.2、GCC 4.2.1 和以下脚本得到了与 ron.rothman 相同的结果。使用它会出现错误吗?
import os, sys
import resource
resource.setrlimit(resource.RLIMIT_NOFILE, (1000,-1))
pureResponseNames = ['f'+str(i) for i in range(434)]
try:
os.mkdir("testCase")
except:
print('Maybe the folder is already there.')
outpathDirTest="testCase/"
output_files = [open(os.path.join(outpathDirTest, fname) + ".txt", "w") for fname in pureResponseNames]
for i in range(len(output_files)):
output_files[i].write('This is a test of file nr.'+str(i))
output_files[i].close()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
71762 次 |
| 最近记录: |