0

Code:

while True:
    print(datetime.datetime.now())
    time.sleep(5)
    print(datetime.datetime.now())

Result:

00:06:53.728000

00:06:58.763000

00:06:58.833000

00:07:03.838000

So why doesn't it come out as: 06:58.728000, 06:58.728000, 07:03:728000?

Kryston
  • 155
  • 1
  • 2
  • 6

1 Answers1

0

The time.sleep documentation is the best:

The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine.

But also:

Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

So, there are no guarantees (this tripped me over as well in the past). You're getting a five second delay (roughly) but the actual delay can be a bit less and/or a bit more than the requested delay.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180