Python Forum
Running loop at specific frequency
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running loop at specific frequency
#1
Hi

I am trying to get my while loop to run at a specific frequency (30Hz) and am having trouble getting it to work. I'm going with 30Hz x 60 seconds should give me a max run of 1800 loops. I am also checking start/end times and taking differences to determine a pause time.

Typical output from my script (I'd like to figure out if this can get close to the 1800 mark):
Output:
Number of loops: 1251 Not paused: 0 Time delta is : 60.01318955421448
This is my test code:
#!/usr/bin/env python

import time
import pause

frequency = 30 # Hz
period = (1.0/frequency)*1000

timeToRun = 1 # how many minutes to run
t_end = time.time() + 60 * timeToRun # run for timeToRun minutes

notPaused = 0
numRuns = 0

elapsedDiffList = []

startTime = time.time()
while time.time() <= t_end:
    timeNowMilli = time.time_ns()/1000000 # time from nanoseconds to milliseconds
    timeEndMilli = time.time_ns()/1000000

    pause.milliseconds(1)

    elapsedDiff = timeEndMilli - timeNowMilli

    if elapsedDiff < period:  # done before our schedule -- pause awhile
        timeToPause = period-elapsedDiff
        pause.milliseconds(timeToPause)
    else:  # took longer than scheduled -- send next message right away without pause
        elapsedDiffList.append(elapsedDiff)  # incase I get an extra long loop
        notPaused += 1
    numRuns += 1

endTime = time.time()
time_delta = (endTime-startTime)
print()
print("Number of loops: " + str(numRuns))
print("Not paused:    ", str(notPaused))
print('Time delta is : ' + str(time_delta))
print(*elapsedDiffList, sep=", ")
It doesn't feel like I'm getting a 30Hz loop out of this. Any idea what I'm missing?

Thanks...
Reply
#2
Alternative - the idea here is to burn cpu cycles until you reach the time to leave the gate. Will work as long as you don't have "not paused" cycles.

import time

start = time.perf_counter()
end_time = start + 60
count = 0

for division in range(1800):
    while time.perf_counter() < start + division/30:
        pass
    count = count + 1
elapsed = time.perf_counter() - start
print(f'Count {count} Elapsed {elapsed}')
Output:
Count 1800 Elapsed 59.966831974
mdsousa likes this post
Reply
#3
Thanks jefsummers

I'm giving this a try. I don't quite understand what it's doing in the line:
while time.perf_counter() < start + division/30:
I'm going to have think about it and see how it gives accurate time pauses.

Thanks...
Reply
#4
time.perf_counter() is the current time in seconds, with the decimal part out to the accuracy of the system
start is the time everything started
division is which "tick" you are on - 60 seconds times 30 ticks per second = 1800 ticks So, division/30 is the amount to add to start to get the time at which the "gate opens"
BTW - can eliminate the end_time line - that was in there when I was going a different path.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  help RuntimeError: no running event loop marpaslight 5 3,696 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  How to save specific variable in for loop in to the database? ilknurg 1 1,141 Mar-09-2022, 10:32 PM
Last Post: cubangt
  bleak library RuntimeError: This event loop is already running alice93 3 4,073 Sep-30-2021, 08:06 AM
Last Post: alice93
  loop running indefinitely shantanu97 6 2,542 Sep-29-2021, 08:03 PM
Last Post: deanhystad
  Running A Loop Until You See A Particular Result knight2000 6 31,656 Sep-04-2021, 08:55 AM
Last Post: knight2000
  RuntimeError: This event loop is already running newbie2019 2 6,947 Sep-30-2020, 06:59 PM
Last Post: forest44
  Running function from parent module which has a loop in it. ta2909i 1 2,678 Nov-18-2019, 07:04 PM
Last Post: Gribouillis
  Delete specific lines contain specific words mannyi 2 4,109 Nov-04-2019, 04:50 PM
Last Post: mannyi
  loop through range until reach size and exclude specific symbol pino88 3 2,362 Sep-23-2019, 02:32 AM
Last Post: perfringo
  How to add coroutine to a running event loop? AlekseyPython 1 8,130 Mar-21-2019, 06:04 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020