For my system I would like to record constantly, which independently I can do. Additionally, I need to be constantly reading some sensors (using SPI), which independently I can also do.

My present problem is combining the two tasks. I need to record all the time, but also read the sensors 10-50 times per second.

Using picamera and camera.start_recording() is usually followed by a sleep() delay. Here are the solutions I plan to explore, but was hoping for some pointers and guidance before diving into each for several hours.

Since I essentially need to record from start to stop of the program, which runs several hours, I was thinking of placing all my sensor reading functions after the .start_recording(), allowing the program only to stop_recording() after my final test conditions have been met (per sensor information).

I could try running two python programs, one recording and one sensing. I believe this would require multi-threading? Or multiprocessing?

Please share your thoughts, and suggestions of other approaches to this problem.

  • 1
    Python might not be the best language choice for this task. It has limited support for multiprocessing and less than stellar performance, unless you can find a python implementation that has these things in abundance. Aug 27, 2018 at 20:49
  • Python is perfectly capable of this task, so long as your timing tolerances are high and you don't need to acquire very quickly (or large images). Python is many times slower than c++, but you just need fast enuf for your task. @RobertHarvey. And if you are a non software engineer, python is a very sensible choice for toy/experimentation and scientific computing etc Aug 28, 2018 at 1:04
  • 1
    note that picamera brings its own background thread to do the image sensor polling when you call start_recording(), as such you are already doing multi-threading in your proposed approach without intending it.
    – crasic
    Aug 28, 2018 at 1:28
  • How important is the timing offset between a camera frame and a sensor sampling? Aug 28, 2018 at 1:46
  • 1
    @LewisPringle: that's not for you to answer. The synchronization requirements only the OP knows, and those requirements will influence how many hoops have to be jumped through for them to be met. Aug 28, 2018 at 18:18

1 Answer 1


Multithreading is a good approach to this problem but is not required.

Logically what you can do is called cooperative multitasking.

Have a loop with time counters for when you last read your sensor and when you last grabbed an image. Define constants for how frequently you must grab an image an hiw frequently you must read sensors. So long as there is enuf tolerance in when you read (there must be some with any approach) you should be all set.

Multithreading can help achieve tighter tolerances on when you read. Also, running two python programs at once can similarly tighten the timing tollerances(as u suggested)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.