0

I am looking for a Pi 2 B to run 3 separate python background tasks simultaneously/constantly: Respond to pin inputs, respond to UDP requests and send UDP data back and poll a set of temperature sensors periodically (say every minute).

What is the best way to handle the polling period without taking significant system resources from the other tasks? Does 'sleep' use significant resources while it is sleeping? Is there a timer interrupt that would use less resources while waiting (and if so how do I program it) - or is that what 'sleep' is anyway? Are there better alternatives?

Grateful for any suggestions. Thanks.

  • You can include both normal UDP sockets and GPIO pins in a poll(), see kernel.org/doc/Documentation/gpio/sysfs.txt. That is the ideal way to do this, since it does not use any resources if nothing is happening, and will respond immediately when something does. I'm not a python user so I can't give you specific details about the equivalent of poll() in python, but there must be one; this is a very fundamental system call. – goldilocks Jun 1 '15 at 11:29
1

Sleep is a good tool for pin monitoring (no cpu usage). Python is a bit memory hungry so you want to keep the process count down but 1GB is a lot to play with so you likely won't have to worry. Use cron for the sensors to reduce memory overhead and coding.

| improve this answer | |
  • Cron executes once a minute so you are not going to get a very timely response to gpio changes. When pigpio samples gpios 0-31 one million times a second it uses roughly 27% of a core. At the default 200 thousand times a second it uses about 7% of a core. – joan Mar 3 '15 at 8:36
  • I think in this circumstance if you need to use one of the userspace libraries such as pigpio you are best to combine it with use of the kernel /proc interface since the latter can be passively polled in "interrupt" mode (but does not provide some of the pi-specific functionality of the former). – goldilocks Jun 1 '15 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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