簡體   English   中英

實施 sched 時參數無效

[英]Invalid Argument when implementing sched

我已經圍繞sched.scheduler編寫了一個基本的包裝器,但是我得到了:

  File "/usr/local/anaconda3/lib/python3.7/sched.py", line 149, in run
    delayfunc(time - now)
OSError: [Errno 22] Invalid argument

這個錯誤的原因是什么? 我正在關注https://docs.python.org/3/library/sched.html

代碼:

#!/usr/local/anaconda3/bin/python

from time import time, sleep
from datetime import datetime, timezone
from sched import scheduler

class Scheduler:
    # specify one or t'other  e.g. "2021-03-17 02:29:00"
    def __init__(self, event_time_utc:str='', local_unix_timestamp_override:float=0):
        self.event_local_timestamp = Scheduler.to_local_unix_timestamp(event_time_utc) \
            if event_time_utc else local_unix_timestamp_override

        # self.seconds_until_event = event_local_timestamp - time()

        print(self.event_local_timestamp - time())

        self.sch = scheduler()

    def run(self, blocking=False):
        self.sch.run(blocking)


    @staticmethod
    def to_local_unix_timestamp(time_utc: str):
        dt_utc = datetime.strptime(time_utc, "%Y-%m-%d %H:%M:%S")
        dt_local = dt_utc.replace(tzinfo=timezone.utc)
        return dt_local.timestamp()


    @staticmethod
    def to_human_readable(unix_timestamp):
        return datetime.fromtimestamp(unix_timestamp).strftime('%Y-%m-%d %H:%M:%S.%f')


    def schedule(self, offset_from_event_time_s:float, callback):
        print('Event scheduled for local unix timestamp:', self.event_local_timestamp + offset_from_event_time_s)

        self.sch.enterabs(
            time = self.event_local_timestamp + offset_from_event_time_s,
            priority = 1,
            action = callback
            )


if __name__ == '__main__':
    sch = Scheduler(local_unix_timestamp_override=time()+5)

    print(f'Started at time: {Scheduler.to_human_readable(time())}')

    def callback():
        print(f'Callback at time: {Scheduler.to_human_readable(time())}')

    sch.schedule(offset_from_event_time_s=1, callback=callback)

    sch.run(blocking=True)

Output:

> ./scheduler.py 
4.999997854232788
Started at time: 2021-03-17 11:00:03.424191
f 1615953609.424139
Traceback (most recent call last):
  File "./scheduler.py", line 55, in <module>
    sch.run(blocking=True)
  File "./scheduler.py", line 20, in run
    self.sch.run(blocking)
  File "/usr/local/anaconda3/lib/python3.7/sched.py", line 149, in run
    delayfunc(time - now)
OSError: [Errno 22] Invalid argument

問題的根源是您假設sched模塊正在使用 time.time() 作為其計時 function。 這是一個錯誤的假設。 它默認使用 time.monotonic() ,它返回自程序啟動以來的時間。 這將是一個很小的數字,例如 6。當您嘗試延遲到 1615644884.1231558 時,這會讓 go 變得瘋狂。

改變這個:

        self.sch = scheduler()

對此:

        self.sch = scheduler(time)

我嘗試調試該問題,發現 time.sleep() 中允許的浮點值的最大長度為 8(我找不到任何資源說明為什么只有 8),但由於您使用的是時間戳,因此該值超過了該值。 您可以在您的機器上嘗試以下代碼並檢查:

import time

# Will work fine
time.sleep(16156448)
    
# Will result in error
time.sleep(1615644884.1231558)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM