HI @David237054 ,
If you are looking for ‘absolutely precise’ and ‘consistent’ times, you will not get that. You are working within a mutli-user, multi-tasking operating system - the Pi uses a version of Linux/Unix. Accuracy to a second - probably ok, to milliseconds, maybe … , to microsecond, no.
The OS is busy doing other tasks as well as running your camera program, and the inherent time-slicing/time-sharing system processes attempt to allocate ‘time’ evenly/appropriately/fairly … . .
If you run these commands at the command prompt you will see all the other processes that want to share time with your camera program.
ps # this will probably on show 2-3 processes
ps -ax # This will show a LOT od processes
ps -ax | wc -l # this will give a count of the lines returned
On my Pi 4, the last command returns a count of 200 processes, I have geany, thonny and a web browser running, all within the GUI environment ( a time-hungry beast), but if you scroll through the output from the ps -ax command you will see a heap more of other system processes - all also wanting access to the processor.
I hope that you can see that it is not simple to get very precise timings…
You can increase the priority of your program by using the ‘nice’ command as shown when launching your program from the command line ( this ‘removes’ all the Thonny processes from the list of things wanting processor time)
sudo nice -n -19 python3 your_program.py
Launching python as above
mjt@raspberrypi:~ $ sudo nice -n -20 python3
Python 3.9.2 (default, Mar 12 2021, 04:06:34)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
I have extracted some lines from a slightly extended ps command after launching with the nice command
mjt@raspberrypi:~ $ ps axl | grep python
.....
0 1000 1055 824 20 0 145928 57436 futex_ Sl ? 946:47 /usr/bin/python3 /usr/bin/thonny
....
4 0 14656 14416 20 0 13016 4040 - S+ pts/1 0:00 sudo nice -n -20 python3
4 0 14657 14656 0 -20 15304 7464 - S<+ pts/1 0:00 python3
....
0 1000 14660 14571 20 0 7448 488 pipe_r S+ pts/2 0:00 grep --color=auto python
mjt@raspberrypi:~ $
You can see the -20 in the 6th (priority) column in the display – everything else is running at the default priority 0.
See the info from the ‘man nice’ manual page. Note you must sudo to get the higher -negative nice values as they are restricted to the root user.
hope this helps
Murray
p.s. you can try this
import os
os.nice(20) # will 'reduce' the process priority to the lowest level
# not what you want ;-(
# maybe IF the program is launched using sudo python3 ....
# THEN you might be able to use
os.nice(-19)
Not tried but might be worthwhile
M