elfresno
Posts: 44
Joined: Sun May 21, 2017 5:19 pm

python script to keep the time

Mon Aug 26, 2019 2:09 pm

Hi, I'm working on a script that triggers instructions based on predefined times in a file, so I think I'm going to have to compare these times with a timecode that I generate to keep me on time. The problem is that I don't know how to run a timecode, and how to make comparisons in the script.

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: python script to keep the time

Mon Aug 26, 2019 2:18 pm

elfresno wrote: Hi, I'm working on a script that triggers instructions based on predefined times in a file, so I think I'm going to have to compare these times with a timecode that I generate to keep me on time. The problem is that I don't know how to run a timecode, and how to make comparisons in the script.

You don't mention what Operating System is running on your unspecified model of RPi board, but doesn't the Operating System keep sufficiently accurate time for your purposes?

elfresno
Posts: 44
Joined: Sun May 21, 2017 5:19 pm

Re: python script to keep the time

Mon Aug 26, 2019 3:21 pm

I am using Raspbian Jessi Lite in rpi3. I was using time sleep between the instructions, and between triggering the instructions and looking for the values ​​of the new times and values ​​there is time that is not counted and throughout the script it will be accumulating, if you know a better way to do it, tell me Thank you

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: python script to keep the time

Mon Aug 26, 2019 3:33 pm

elfresno wrote:
Mon Aug 26, 2019 3:21 pm
I am using Raspbian Jessi Lite in rpi3. I was using time sleep between the instructions, and between triggering the instructions and looking for the values ​​of the new times and values ​​there is time that is not counted and throughout the script it will be accumulating, if you know a better way to do it, tell me Thank you
Several suggestions. But no simple answer.


You are working with a version of the Raspbian Operating System that was superceded two years ago, and whose replacement has in turn been superceded.


Raspbian (and Debian from which it is derived) is a multi-user time-sharing operating system with its own scheduler. It is not a Real Time Operating System: perhaps it will never have the accuracy you require?


Perhaps posting your particular script might enable someone knowledgeable to make directly relevant comments. (Don't overlook the need to use the forum Code tag markup to maintain formatting.)

gordon77
Posts: 5036
Joined: Sun Aug 05, 2012 3:12 pm

Re: python script to keep the time

Mon Aug 26, 2019 3:35 pm

Try time.time()

Code: Select all

import time
start = time.time()

while True:
    if time.time() - start >= 1:
        print ("1 second") 
        start = time.time()
Last edited by gordon77 on Mon Aug 26, 2019 4:01 pm, edited 1 time in total.

User avatar
scruss
Posts: 3212
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: python script to keep the time

Mon Aug 26, 2019 3:49 pm

Apart from being a couple of OS releases behind what folks are comfortable supporting here, I have to ask:
  • how frequently? Is this multiple times a second, once every few seconds, …? If it only need to be done once a minute or less frequently, cron might do it
  • how accurately? right that microsecond (difficult), or whenever the computer finds time (easy)?
  • Are you syncing time from an external source, or using a cue/SRT file to generate times that events have to trigger?
  • Is there a chance that the tasks might still be running at the time you need to start the next task?
As you've found, sleep isn't the best as it doesn't take into account how long your task took to run. Maybe look at pause.until() from the Python pause package?
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

elfresno
Posts: 44
Joined: Sun May 21, 2017 5:19 pm

Re: python script to keep the time

Tue Aug 27, 2019 3:50 pm

hello thanks for answering, I am thinking about the project so I have nothing very specific, I want the timecode to be generated in the script, with some library, if it exists. I am thinking about frame resolution, milliseconds, I was seeing the datetime function that has an answer in milliseconds, which is not the function time (seconds only). Do you think it is possible?

blimpyway
Posts: 350
Joined: Mon Mar 19, 2018 1:18 pm

Re: python script to keep the time

Tue Aug 27, 2019 5:29 pm

time.time() returns a float, it returns seconds with 6 decimal places, which means microseconds:

Code: Select all

 $ python
Python 3.6.1 | packaged by rpi | (default, Apr 20 2017, 19:35:19) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> time.time()
1566926863.543218
>>> 
or minimum time between two consecutive invocations:

Code: Select all

>>> time.time() - time.time() 
-8.58306884765625e-06
which is an 8.6 microseconds "resolution".

Return to “Python”