【问题标题】:What is the value of C _PyTime_t in PythonPython 中 C _PyTime_t 的值是多少
【发布时间】:2017-08-16 02:34:06
【问题描述】:

在 Python 3 中长时间休眠(如运行 time.sleep(3**3**3))时,程序会返回一个 OverflowError 并带有错误消息“timestamp too large to convert to C _PyTime_t”。我能睡多久?

【问题讨论】:

    标签: python time


    【解决方案1】:

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

    PEP 564 中提及_PyTime_t

    CPython 私有“pytime”C API 处理时间现在使用新的 _PyTime_t 类型:简单的 64 位有符号整数 (C int64_t)。 _PyTime_t 单元是一个实现细节,而不是 API 的一部分。当前单位为 1 纳秒。

    >>> 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
    >>> 
    

    【讨论】:

      【解决方案2】:

      我从documentation of the threading library找到了以下内容:

      threading.TIMEOUT_MAX

      阻塞函数(Lock.acquire()、RLock.acquire()、Condition.wait()等)的超时参数允许的最大值。指定大于此值的超时将引发溢出错误。

      3.2 版中的新功能。

      在我的系统上,使用 python 3.8:

      >>> import threading
      >>> threading.TIMEOUT_MAX
      9223372036.0
      

      我没有看到它在任何地方明确指定 threading.TIMEOUT_MAXtime.sleep() 的参数的最大值,但它似乎是正确的值(或“关闭”,出于某种原因),构造相同约束。

      【讨论】:

        猜你喜欢
        • 2014-06-04
        • 1970-01-01
        • 2016-11-22
        • 1970-01-01
        • 2017-10-02
        • 1970-01-01
        • 1970-01-01
        • 2011-01-22
        相关资源
        最近更新 更多