Skip to main content
 首页 » 编程设计

python之Python 中 C _PyTime_t 的值

2025年02月15日8kuangbin

当在 Python 3 中长时间休眠(如运行 time.sleep(3**3**3))时,程序会返回一个 OverflowError,错误消息为“timestamp too large to转换为 C _PyTime_t”。我最多可以睡多长时间?

请您参考如下方法:

该值应为 9223372036.854775,即“是计算中 64 位有符号整数的最大值”。参见 this Wikipedia article .

Mentioning of _PyTime_t in PEP 564:

The CPython private "pytime" C API handling time now uses a new _PyTime_t type: simple 64-bit signed integer (C int64_t). The _PyTime_t unit is an implementation detail and not part of the API. The unit is currently 1 nanosecond.

>>> 2 ** 63 / 10 ** 9 
9223372036.854776 
>>> time.sleep(9223372036.854775) 
^CTraceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
KeyboardInterrupt 
>>> time.sleep(9223372036.854776) 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
OverflowError: timestamp too large to convert to C _PyTime_t 
>>>