IOError: [Errno 22] 传入的时钟() 参数无效

发布于 2024-09-09 12:30:28 字数 431 浏览 3 评论 0原文

我没有找到很好的解释来解释什么是无效参数错误以及什么会导致它们。

我当前正在使用的示例是

import sys
mylog="mylog.log"
sys.stdout = open(mylog,'w')
#lots of code
#.
#.
#.
#End of lots of code
from time import clock
print "blablabla",clock()

我在时钟线上收到 IOError Invalid Argument 错误。我也尝试过

print "blablabla\t%s"%clock()

任何有关此错误的信息都会有很大帮助。这些行在短期运行时工作得很好,只是在运行代码一段时间后就中断了。我尝试将缓冲区大小设置为较低的值,例如 45-100 行。

I have not had much luck hunting for finding a good explanation of what invalid argument errors are and what would cause them.

My current sample I am working with is

import sys
mylog="mylog.log"
sys.stdout = open(mylog,'w')
#lots of code
#.
#.
#.
#End of lots of code
from time import clock
print "blablabla",clock()

I receive an IOError Invalid Argument error on the clock line. I have also tried

print "blablabla\t%s"%clock()

Any information about this error would be great help. Those lines work perfectly fine on short runs, it just after running the code for a while it breaks. I have tried to setting the buffer size to something low like 45-100 lines.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

じ违心 2024-09-16 12:30:33

我无法在自己的计算机上重现这个确切的问题,所以我无法给出具体的建议,但这里有一些关于如何调试此类事情的一般评论。

当您在 python 的 IOError 或 OSError 异常中看到“无效参数”时,这意味着解释器尝试进行系统调用,但失败并将 errno 设置为代码 EINVAL 。 (顺便说一句,Python 确实不应该打印 errno 代码的数值 - 符号名称是标准化的,但数字不是。)您需要做的第一件事是找出它是哪个系统调用,以及最简单的方法方法是在 strace 实用程序下运行您的程序,如下所示:

$ strace -f -o strace.log python yourscript.py [arguments...]

等待它失败,然后在文件 strace.log 中搜索“-1 E”(正是细绳)。您会发现类似这样的内容:

times({tms_utime=162, tms_stime=123, tms_cutime=0, tms_cstime=0}) = 1718279979
write(1, "2.85\n", 5)                   = -1 EINVAL (Invalid argument)

然后,您阅读失败的系统调用的手册页(本例中为“man 2 write”)并查找 errno 代码名称 (EINVAL code> 在本例中),看看它说出了什么问题。

在这种情况下,我强烈怀疑您在 Python 解释器或操作系统中发现了错误。 “无效参数”的意思就是它所说的 - 系统调用的输入参数之一具有无效值。您没有在脚本中执行任何棘手的操作,因此要么解释器弄乱了其系统调用,要么内核误解了解释器想要的内容。

I can't reproduce this exact problem on my own computer, so I can't give specific advice, but here is some general commentary on how to debug this sort of thing.

When you see "Invalid argument" in an IOError or OSError exception from python, that means the interpreter tried to make a system call, which failed and set errno to the code EINVAL. (Tangentially, python really shouldn't print the numeric values for errno codes - the symbolic names are standardized but the numbers aren't.) The first thing you need to do is find out which system call it was, and the easiest way to do that is run your program under the strace utility, like this:

$ strace -f -o strace.log python yourscript.py [arguments...]

Wait for it to fail, then search the file strace.log for "-1 E" (exactly that string). You will find something like this:

times({tms_utime=162, tms_stime=123, tms_cutime=0, tms_cstime=0}) = 1718279979
write(1, "2.85\n", 5)                   = -1 EINVAL (Invalid argument)

You then read the man page for the system call that failed ("man 2 write" in this case) and look for the errno code name (EINVAL in this case), and see what it says has gone wrong.

In this case I strongly suspect you have found a bug in either the Python interpreter or the operating system. "Invalid argument" means what it says - one of the input arguments to the system call had an invalid value. You're not doing anything tricky in your script, so either the interpreter is messing up its system calls, or the kernel misunderstood what the interpreter wanted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文