What is the value of C _PyTime_t in Python

左心房为你撑大大i 提交于 2021-02-20 18:57:26

问题


When sleeping for a long time (like running time.sleep(3**3**3)) in Python 3, the program returns an OverflowError with the error message "timestamp too large to convert to C _PyTime_t". What is the largest time length I can sleep?


回答1:


The value should be 9223372036.854775, which is "is the maximum value for a 64-bit signed integer in computing". See 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
>>> 



回答2:


I found the following from the documentation of the threading library:

threading.TIMEOUT_MAX

The maximum value allowed for the timeout parameter of blocking functions (Lock.acquire(), RLock.acquire(), Condition.wait(), etc.). Specifying a timeout greater than this value will raise an OverflowError.

New in version 3.2.

On my system, with python 3.8:

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

I don't see it clearly specified anywhere that threading.TIMEOUT_MAX is a maximum for the argument to time.sleep(), but it appears to be the right value (or "close", for some reason), constructed with the same constraints.



来源:https://stackoverflow.com/questions/45704243/what-is-the-value-of-c-pytime-t-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!