How to schedule a periodic task that is immune to system time change using Python

Vincent

I am using python's sched module to run a task periodically, and I think I have come across a bug.

I find that it relies on the time of the system on which the python script is run. For example, let's say that I want to run a task every 5 seconds. If I forward the system time, the scheduled task will run as expected. However, if I rewind the system time to, say 1 day, then the next scheduled task will run in 5 seconds + 1 day.

If you run the script below and then change your system time by a few days back, then you can reproduce the issue. The problem can be reproduced on Linux and Windows.

import sched
import time
import threading

period = 5
scheduler = sched.scheduler(time.time, time.sleep)

def check_scheduler():
    print time.time()
    scheduler.enter(period, 1, check_scheduler, ())

if __name__ == '__main__':
    print time.time()

    scheduler.enter(period, 1, check_scheduler, ())

    thread = threading.Thread(target=scheduler.run)
    thread.start()
    thread.join()
    exit(0)

Anyone has any python solution around this problem?

Dan Cornilescu

From the sched documentation:

class sched.scheduler(timefunc, delayfunc)

The scheduler class defines a generic interface to scheduling events. It needs two functions to actually deal with the “outside world” — timefunc should be callable without arguments, and return a number (the “time”, in any units whatsoever). The delayfunc function should be callable with one argument, compatible with the output of timefunc, and should delay that many time units. delayfunc will also be called with the argument 0 after each event is run to allow other threads an opportunity to run in multi-threaded applications.

The problem you have is that your code uses time.time() as timefunc, whose return value (when called without arguments) is the current system time and is thus affected by re-winding the system clock.

To make your code immune to system time changes you'd need to provide a timefunc which doesn't depend on the system time, start/current timestamps, etc.

You can write your own function, for example one returning the number of seconds since your process is started, which you'd have to actually count in your code (i.e. don't compute it based on timestamp deltas). The time.clock() function might help, if it's based on CPU time counters, but I'm not sure if that's true or not.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Use Task Schedule to offset System Time

分類Dev

Celery - Schedule periodic task at the end of another task

分類Dev

How to set periodic task on Django using celery?

分類Dev

Real time task (periodic task)

分類Dev

How to schedule a task with airflow

分類Dev

Celery periodic task not periodic

分類Dev

How do I schedule an email to send at a certain time using cron and smtp, in python?

分類Dev

Schedule an alarm with AlarmManager using a given time on android

分類Dev

How to keep a schedule alive (created using Quartz & .Net Core) - as it is not working after some time after deployed on IIS

分類Dev

How to show the time when task was created by using react

分類Dev

How to change system time or force a sync with hardware clock

分類Dev

How to change an set of numbers to time [Python]

分類Dev

How to pass a class based task into CELERY_BEAT_SCHEDULE

分類Dev

How to create periodic matrix using single vector in matlab?

分類Dev

ImportError: No module named 'schedule' when i using system to run a service

分類Dev

How to print the local system time using log4cplus?

分類Dev

How to get the windows system boot time using php code

分類Dev

How to schedule multiple notification using alarm manager?

分類Dev

SQLAlchemy: How to change a MySQL server system variable using SQLAlchemy?

分類Dev

How to change the parameters for a task with known task and tasklist ids

分類Dev

How to cancel a Task using CancellationToken?

分類Dev

Thread how to change sleep time

分類Dev

how to create a time change listener

分類Dev

How to ntp server time down to millisecond precision using Python ntplib?

分類Dev

Get RecursionError / KeyError trying to add Periodic Task in Celery beat

分類Dev

Crontab - "bad day-of-week" error, when trying to schedule a task to run each other day-of-week using "dow/2"

分類Dev

Cron schedule a task alleatory in a range of hours

分類Dev

Solving a task in cosmology using Python 2.7.5

分類Dev

Python schedule with commandline

Related 関連記事

  1. 1

    Use Task Schedule to offset System Time

  2. 2

    Celery - Schedule periodic task at the end of another task

  3. 3

    How to set periodic task on Django using celery?

  4. 4

    Real time task (periodic task)

  5. 5

    How to schedule a task with airflow

  6. 6

    Celery periodic task not periodic

  7. 7

    How do I schedule an email to send at a certain time using cron and smtp, in python?

  8. 8

    Schedule an alarm with AlarmManager using a given time on android

  9. 9

    How to keep a schedule alive (created using Quartz & .Net Core) - as it is not working after some time after deployed on IIS

  10. 10

    How to show the time when task was created by using react

  11. 11

    How to change system time or force a sync with hardware clock

  12. 12

    How to change an set of numbers to time [Python]

  13. 13

    How to pass a class based task into CELERY_BEAT_SCHEDULE

  14. 14

    How to create periodic matrix using single vector in matlab?

  15. 15

    ImportError: No module named 'schedule' when i using system to run a service

  16. 16

    How to print the local system time using log4cplus?

  17. 17

    How to get the windows system boot time using php code

  18. 18

    How to schedule multiple notification using alarm manager?

  19. 19

    SQLAlchemy: How to change a MySQL server system variable using SQLAlchemy?

  20. 20

    How to change the parameters for a task with known task and tasklist ids

  21. 21

    How to cancel a Task using CancellationToken?

  22. 22

    Thread how to change sleep time

  23. 23

    how to create a time change listener

  24. 24

    How to ntp server time down to millisecond precision using Python ntplib?

  25. 25

    Get RecursionError / KeyError trying to add Periodic Task in Celery beat

  26. 26

    Crontab - "bad day-of-week" error, when trying to schedule a task to run each other day-of-week using "dow/2"

  27. 27

    Cron schedule a task alleatory in a range of hours

  28. 28

    Solving a task in cosmology using Python 2.7.5

  29. 29

    Python schedule with commandline

ホットタグ

アーカイブ