1

I would like to execute one line at a exact second, say 14:02:08.000000. Here's the code I used to do so:

now  = datetime.now()
while now.strftime("%f")!= "000000":
   now  = datetime.now()
run_the_code

This method works fine on my laptop. However, when i use this code on my raspberry Pi, which has a much slower CPU, it takes quite a while to 'catch' the case of %f == 000000.

I guess the sched method would be faster, but I don't know how to make it run on the exact time of %f == 000000.

Is there another way to work around it on my slow RPi? Thanks!

user3768495
  • 4,077
  • 7
  • 32
  • 58
  • Running a while loop with no delay will use up as much cpu as it can get. I'm not sure if raspberry pu has it, but you might just want to use a cron job to run a python script. – EvergreenTree Feb 23 '15 at 22:26
  • 3
    You might want to learn a bit about [cron](http://www.raspberrypi.org/documentation/linux/usage/cron.md) if your goal is to just execute some command at some arbitrary time. – aruisdante Feb 23 '15 at 22:26
  • 1
    If you're trying to schedule events within a module itself, you can also look into [`sched`](https://docs.python.org/2/library/sched.html), though it's not terribly precise. It also expects to have basically all runtime control of execution in the module. See also here: http://stackoverflow.com/questions/23170318/real-time-interrupts-in-python – aruisdante Feb 23 '15 at 22:30
  • 2
    @aruisdante without a real-time OS, *nothing* you can do is terribly precise. The question presumes you can target a time interval of a single microsecond, and that's going to be impossible. – Mark Ransom Feb 23 '15 at 22:32
  • @MarkRansom I meant it's not terribly precise because it's limited 1) by the execution speed of your `timefunc` and `delayfunc`, and 2) Because it's single-threaded, so an event firing blocks other events from firing on schedule. I wasn't even touching the implementation resolution of `timefunc` and `delayfunc` on a particular platform. You're absolutely correct, you're not getting microsecond timing on a Rasp Pi no matter what you do. I suppose I should have said *accurate*, not *precise* (in the measurement definitions of those two words) – aruisdante Feb 23 '15 at 22:33
  • Now I understand why I have this problem. My laptop is fast so the interval between each repetition of now = data.now() is about 0.003 second. So it can easily catch a moment of now.strftime("%f")!= "000000". That is not the case on RPi which is much slower. – user3768495 Feb 25 '15 at 17:58

1 Answers1

0

Well, there is no way to schedule events at exact second, but maybe you would want to run at or slightly after the next second, thus:

from datetime import timedelta, datetime

next_second = datetime.now() + timedelta(microseconds=999999)
next_second = next_second.replace(microsecond=0)
while datetime.now() < next_second:
    pass

The code rounds the current time up to the next full second:

>>> now = datetime.now()
>>> now
datetime.datetime(2015, 2, 24, 0, 30, 9, 885974)
>>> next_second = now + timedelta(microseconds=999999)
>>> next_second
datetime.datetime(2015, 2, 24, 0, 30, 10, 885973)
>>> next_second.replace(microsecond=0)
datetime.datetime(2015, 2, 24, 0, 30, 10)