Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 实现sched时参数无效_Python_Scheduler_Invalid Argument_Sched - Fatal编程技术网

Python 实现sched时参数无效

Python 实现sched时参数无效,python,scheduler,invalid-argument,sched,Python,Scheduler,Invalid Argument,Sched,我已经编写了一个关于sched.scheduler的基本包装,但是我得到了: File "/usr/local/anaconda3/lib/python3.7/sched.py", line 149, in run delayfunc(time - now) OSError: [Errno 22] Invalid argument 这个错误的原因是什么?我在跟踪 代码: #/usr/local/anaconda3/bin/python 从时间导入时间,睡眠 从日

我已经编写了一个关于
sched.scheduler
的基本包装,但是我得到了:

  File "/usr/local/anaconda3/lib/python3.7/sched.py", line 149, in run
    delayfunc(time - now)
OSError: [Errno 22] Invalid argument
这个错误的原因是什么?我在跟踪

代码:

#/usr/local/anaconda3/bin/python
从时间导入时间,睡眠
从日期时间导入日期时间,时区
从sched导入计划程序
类计划程序:
#指定一个或另一个,例如“2021-03-17 02:29:00”
定义初始化(self,事件时间utc:str='',本地unix时间戳覆盖:float=0):
self.event\u local\u timestamp=Scheduler.to\u local\u unix\u timestamp(event\u time\u utc)\
如果事件\u时间\u utc else本地\u unix\u时间戳\u覆盖
#self.seconds\u until\u event=event\u local\u timestamp-time()
打印(self.event\u local\u timestamp-time())
self.sch=scheduler()
def运行(自阻塞=假):
自润滑运行(阻塞)
@静力学方法
def到本地unix时间戳(时间utc:str):
dt_utc=datetime.strtime(time_utc,“%Y-%m-%d%H:%m:%S”)
dt_local=dt_utc.replace(tzinfo=timezone.utc)
返回dt_local.timestamp()
@静力学方法
def到人类可读(unix时间戳):
return datetime.fromtimestamp(unix\u timestamp).strftime(“%Y-%m-%d%H:%m:%S.%f”)
def计划(自身、从事件到时间的偏移量:浮动、回调):
打印('Event scheduled for local unix timestamp:'、self.Event\u local\u timestamp+offset\u from\u Event\u time\s)
自我教育(
时间=self.event\u local\u时间戳+与事件时间的偏移量,
优先级=1,
动作=回调
)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
sch=调度程序(本地unix时间戳覆盖=时间()+5)
打印(f'开始于时间:{Scheduler.to_human_readable(time())}'))
def callback():
打印(f'Callback at time:{Scheduler.to_human_readable(time())}'))
附表(从事件到时间的偏移量=1,回调=回调)
sch.run(阻塞=真)
输出:

> ./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
./scheduler.py
4.999997854232788
开始时间:2021-03-17 11:00:03.424191
f 1615953609.424139
回溯(最近一次呼叫最后一次):
文件“/scheduler.py”,第55行,在
sch.run(阻塞=真)
文件“/scheduler.py”,第20行,正在运行
自润滑运行(阻塞)
文件“/usr/local/anaconda3/lib/python3.7/sched.py”,第149行,运行中
delayfunc(时间-现在)
OSError:[Errno 22]参数无效

我尝试调试该问题,发现time.sleep()中允许的浮点值的最大长度为8(我找不到任何关于为什么只有8的资源),但由于您使用的是时间戳,因此该值超过了该值。您可以在机器上尝试以下代码并检查:

import time

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

问题的根源在于您假设
sched
模块使用time.time()作为其计时函数。这是一个错误的假设。默认情况下,它使用time.monotic(),它返回自程序启动以来的时间。这将是一个小数字,比如6。当您试图延迟到1615644884.1231558时,这会让它变得疯狂

更改此项:

        self.sch = scheduler()
为此:

        self.sch = scheduler(time)

虽然这很有趣,但这不是问题的根源。他真的不想等16亿秒……是的,在看了你的答案后,他明白了要点