对Python中的时区更改计时器不敏感

对Python中的时区更改计时器不敏感,python,timer,timezone,Python,Timer,Timezone,在我的外围设备中,我使用python定时器: timer = Timer(45, my_func, []) timer.start() 问题是在计时器运行期间,可以更改设备时区(由于设备已连接到WIFI),计时器将立即停止 是否存在对时区变化不敏感的另一种方式 我使用Python3.7.3您可以使用计时器包装函数,只需使用常规线程即可。 示例代码: from threading import Thread import time def wrapper_func(seconds: flo

在我的外围设备中,我使用python定时器:

timer = Timer(45, my_func, [])
timer.start()
问题是在计时器运行期间,可以更改设备时区(由于设备已连接到WIFI),计时器将立即停止

是否存在对时区变化不敏感的另一种方式


我使用Python3.7.3

您可以使用计时器包装函数,只需使用常规线程即可。
示例代码:

from threading import Thread
import time


def wrapper_func(seconds: float, func, args: dict, sleep_interval_seconds: float = 0.1):
    seconds_left = seconds
    while seconds_left >= 0:
        time.sleep(sleep_interval_seconds)
        seconds_left -= sleep_interval_seconds
    if args:
        func(**args)
    else:
        func()


def timed_thread(seconds: float, target, func_args: dict = None) -> Thread:
    return Thread(target=wrapper_func, args=(seconds, target, func_args))


def funcc():
    print("BBBBBB")


t = timed_thread(3, funcc)
t.start()

print("AAAAA")
time.sleep(4)
print("CCCCC")
将打印:

AAAAA
BBBBBB
CCCCC