我希望我的Python2守护程序醒来并在第二个步骤做一些准确的事情。

是否有更好的方法可以获取除 sleep 时间以外的浮点数的小数部分:

now = time.time()               #get the current time in a floating point format
left_part = long(now)           #isolate out the integer part
right_part = now - left_part    #now get the decimal part
time.sleep(1 - right_part)      #and sleep for the remaining portion of this second.

sleep 时间是可变的,具体取决于此秒内完成的工作量。

我不知道有没有“睡到”功能?或者,
有没有更好的方法来解决这个问题?

我希望守护程序尽可能地高效,以免从其他进程中垄断过多的CPU。

谢谢。标记。

最佳答案

使用 math.modf()



示例:

>>> from math import modf
>>> modf(3.1234)
(0.12340000000000018, 3.0)

关于python - 在Python中获取浮点数的小数部分,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30090072/

10-12 17:42
查看更多