简体   繁体   English

打印与写入期间IOError的可能性

[英]Likelihood of IOError during print vs. write

I recently encountered an IOError writing to a file on NFS. 我最近在写入NFS上的文件时遇到IOError。 There wasn't a disk space or permission issue, so I assume this was just a network hiccup. 没有磁盘空间或权限问题,所以我认为这只是网络故障。 The obvious solution is to wrap the write in a try-except, but I was curious whether the implementation of print and write in Python make either of the following more or less likely to raise IOError: 显而易见的解决方案是将写操作包装在try-except中,但是我很好奇Python中print和write的实现是否或多或少会引起IOError:

f_print = open('print.txt', 'w')
print >>f_print, 'test_print'
f_print.close()

vs.

f_write = open('write.txt', 'w')
f_write.write('test_write\n')
f_write.close()

(If it matters, specifically in Python 2.4 on Linux). (如果重要的话,特别是在Linux上的Python 2.4中)。

prints are implemented in terms of writes which ultimately result in a write(2) call to the kernel. 打印是根据写入实现的,最终导致对内核的write(2)调用。 You could run strace on those two samples and (after wading through a lot of chaff) see the same resultant calls to write(2). 您可以在这两个样本上运行strace ,并且(在经过大量实验之后)看到对write(2)的相同结果调用。

Indeed, I just did that and omitting 2000+ lines of output yielded: 确实,我只是这样做了,并且省略了2000多个输出行:

execve("/usr/bin/python", ["python", "a.py"], [/* 43 vars */]) = 0
open("print.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
write(3, "test_print\n", 11)            = 11
close(3)                                = 0

and

execve("/usr/bin/python", ["python", "b.py"], [/* 43 vars */]) = 0
open("write.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 3
write(3, "test_write\n", 11)            = 11
close(3)                                = 0

not a whole lot of difference to be seen, there. 那里没有太多差异可以看到。 Whether the destination file is on a local disk or an NFS mount, the write() call will be the same. 无论目标文件是在本地磁盘上还是在NFS挂载上,write()调用都将是相同的。 The oft-named Nightmare File System will - all other things being equal - fail more often than your local disk. 在其他所有条件不变的情况下,经常被称为“噩梦文件系统”的故障比本地磁盘故障的可能性更大。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM