1

Question

I need to make a short write to a file every 15 seconds (and sleep the rest of the time)... it seems to me that multithreading or multiprocessing would be useful to address this, by having a dedicated thread or process to do the file write. Which would be better in terms of timing/reliability, as well as memory footprint?

Background

I am writing a small Python application for a Chumby (so limited memory availability -- 128MB total system memory); to stop the default Chumby control panel from restarting after I've killed it, a temp file needs to be written to every 15 seconds or so to "fool" the watchdog process that would ordinarily restart the control panel. The main application may be busy doing other things, and I don't want to try to have to "watch the clock" as it's doing its other things to make sure it squeezes in the temp file write.

Community
  • 1
  • 1
Doktor J
  • 1,058
  • 1
  • 14
  • 33

1 Answers1

4

The Chumby seems to be Linux-based, so signal.setitimer() should be available (provided you have access to Python 2.6 or above).

This function allows you to install a handler that is periodically called, so you don't need a thread or process.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • Cool, that might be useful. I'll try it out when I get home! – Doktor J Feb 13 '12 at 17:20
  • to make sure I understand this... I would use `signal.signal(signal.SIGUSR1,writetemp)` (where writetemp is a handler I've defined that does the file writing), and `signal.setitimer(signal.ITIMER_REAL,15,15)` to initialize the interval? – Doktor J Feb 13 '12 at 17:48
  • @DoktorJ: Did you read the linked documentation? The signal corresponding to `ITIMER_REAL` is `SIGALRM`. – Sven Marnach Feb 13 '12 at 17:51
  • sorry, didn't catch that part (was just doing some research on my lunch break at work)... so I can't send other signals to the application? would SIGALRM have any detrimental effect on the app as long as I've got the handler set up? – Doktor J Feb 13 '12 at 18:36