为什么 time.sleep 完全停止执行?

Ale*_*exC 0 python python-2.7

以下函数旨在运行循环 20 分钟,处理 SQL 任务(如果可用)。为了避免在没有什么可处理的情况下过多的 SQL 调用,它打算在尝试再次处理任务之前休眠 5 秒:

def main():
    sql = 'some sql task here;'
    stop_time = datetime.today() + timedelta(minutes = 20)
    print('Started at ', datetime.now())
    print('Should stop at', stop_time)
    load_more_rows = True
    with ConnectionParameters.get_conn() as conn:
        while load_more_rows or (datetime.now() < stop_time):
            try:
                res = get_num_processed_batches(conn, sql)
                processed_batch = res > 0
            except Exception as ex:
                log_error(repr(ex))
                processed_batch = False
            if not processed_batch:
                print('Sleeping at ', datetime.now())
                time.sleep(seconds=5)
            load_more_rows = processed_batch
            print('Finished iteration at ', datetime.now())
    print('Stopped at ', datetime.now())
Run Code Online (Sandbox Code Playgroud)

不幸的是,它并没有休眠、醒来并继续处理任务直到时间到,而是完全停止执行,如输出所示:

('Started at ', datetime.datetime(2015, 3, 31, 17, 31, 6, 206652))
('Should stop at', datetime.datetime(2015, 3, 31, 17, 51, 6, 206630))
('Finished iteration at ', datetime.datetime(2015, 3, 31, 17, 31, 6, 356698))
('Finished iteration at ', datetime.datetime(2015, 3, 31, 17, 31, 6, 614349))
('Finished iteration at ', datetime.datetime(2015, 3, 31, 17, 31, 6, 638210))
('Finished iteration at ', datetime.datetime(2015, 3, 31, 17, 31, 6, 765645))
('Finished iteration at ', datetime.datetime(2015, 3, 31, 17, 31, 6, 885282))
('Finished iteration at ', datetime.datetime(2015, 3, 31, 17, 31, 7, 12109))
('Sleeping at ', datetime.datetime(2015, 3, 31, 17, 31, 7, 13803))
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

Tho*_*ers 5

time.sleep不接受关键字参数seconds

time.sleep(5)
Run Code Online (Sandbox Code Playgroud)

我不确定为什么它不会引发有关缺少必需参数的错误。

https://docs.python.org/2/library/time.html#time.sleep

>>> time.sleep(seconds=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sleep() takes no keyword arguments
>>> time.sleep(1)
>>> "everything is fine"
Run Code Online (Sandbox Code Playgroud)