我在64位Mac OS X机器上使用python 2.7.10
。为什么time.sleep()
的{{1}}值超过100000000?
在下面的示例中,IOError
有效,但是time.sleep(100000000)
引发time.sleep(100000001)
IOError
答案 0 :(得分:3)
此行为特定于macOS,而非Python。 Python uses the select()
function to implement sleep
,如果您尝试以超过1e8秒的超时时间运行select
,则Perl在macOS上也会表现出相同的行为:
# perl -E 'select $x, $x, $x, 100000001; say $!'
Invalid argument
XNU内核中的itimerfix
函数似乎是这种行为的罪魁祸首:
/*
* Check that a proposed value to load into the .it_value or
* .it_interval part of an interval timer is acceptable.
*/
int
itimerfix(
struct timeval *tv)
{
if (tv->tv_sec < 0 || tv->tv_sec > 100000000 ||
tv->tv_usec < 0 || tv->tv_usec >= 1000000)
return (EINVAL);
return (0);
}