How to delay until a next increment of time occurs ?

Rob Gaddi rgaddi at highlandtechnology.invalid
Wed Nov 13 13:30:14 EST 2019


On 11/13/19 6:02 AM, R.Wieser wrote:
> Hello all,
> 
> I'm writing some code to toggle a pin on a Raspberry Pi, and would like to
> have that happen at (multiples of) 300 uSec increments.
> 
> I tried time.sleep(), but that one disregards the time after the last one
> ends and the new one is started.  In other words, all the time spend in code
> (or being interrupted by another thread!) between the end of the previous
> and the start of the current sleep throws the whole repetitive timing off.
> 
> So, I'm looking for a method that will allow me to wait for a "last time
> plus increment".  Is there one with the properties of sleep() (not just
> burning processor cycles way, blocking all threads), but referencing a
> previous time.
> 
> Regards,
> Rudy Wieser
> 
> 

300us is getting on towards realtime.  Depending on how rigorous you need to be, 
Python may not be the right tool for the job; I've never tried it that fast.

That said, I'll throw out a different option than the threading based ones 
people have suggested.  Trap SIGALRM with signal.signal and use signal.setitimer 
to create an interval timer.

I can't tell you offhand that signals will give you better realtime performance 
than threads or vice versa.  There's also async in the mix, which I still have 
no idea how to use.  But this way if you strike out on one approach you've got 
some others to consider.

Also, does the rPi have any PWM or counter pins that you can just set and 
forget, rather than trying to keep it going yourself?

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.


More information about the Python-list mailing list