3

I writing some code to get sensor readings from GPIO against time. To make sure the measurements corresponds to a specific time, I want to know if python iterates at a constant speed (so that the gap between iterations is constant) - and what is its minimum time gap between iterations.

If they're not, can someone let me know how to make the time gap constant. Thank you!

  • Check this thing out : https://docs.python.org/2/library/time.html#time.struct_time – ZdaR Apr 01 '15 at 11:45
  • 1
    You would need a realtime operating system in order to guarantee constant speed, regardless of language. Or, it depends on how reliable your measurements need to be, as a program running on a general purpose OS would be constant speed most of the time, but not always and no guarantees. – nos Apr 01 '15 at 11:49

1 Answers1

10

No, Python does not and can not iterate at constant speed.

Python is just another process on your Raspberry PI, and your OS is responsible for allocating it time to run on the CPU (called multi-tasking). Other processes also get allotted time. This means Python is never going to be running all the time and any processing times are going to be depend on what the other processes are doing.

Iteration itself is also delegated to specific types; how the next item is produced then varies widely, and even if Python was given constant access to the CPU iteration would still vary. Whatever you do in your loop body also takes time, and unless the inputs and outputs are always exactly the same, will almost certainly take a variable amount of time to do the work.

Instead of trying to time your loops, measure time with time.time() or timeit.default_timer (depending on how precise you need to be, on your Raspberry it'll be the same function) in a loop and adjust your actions based on that.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Could this not be generalised to other languages as well? As it is more related to the underlying OS and the scheduler than to the language in specific? – exhuma Apr 01 '15 at 11:50
  • 2
    @exhuma: this is generalised to all languages running on a generic OS. You'd need a [real-time OS](http://en.wikipedia.org/wiki/Real-time_operating_system) to make guarantees about execution timings. – Martijn Pieters Apr 01 '15 at 11:50
  • I wonder, in what cases would the OS interrupt the process? If there are no exceptions/interrupts, shouldn't the CPU run the process all the time? – khajvah Apr 01 '15 at 11:52
  • 2
    @khajvah: The OS will be constantly switching; otherwise it'd have no time scheduled for making scheduling decisions itself even. – Martijn Pieters Apr 01 '15 at 11:53
  • @martijn-pieters: That's what I meant. Maybe I phrased it badly. What I meant was that the wording of the answer might make it sound like it is a Python issue. – exhuma Apr 01 '15 at 11:53
  • 1
    @khajvah: take a look at any Linux or Windows system and see how many services or daemons are running, not to mention the kernel itself. There are far more active threads/processes than there are CPU cores. – cdarke Apr 01 '15 at 11:58