Code: Select all
import threading # For Threads and Locks import time # For time.sleep() class RoomHandler: def __init__(self, ...): self.logFileLock = threading.Lock() blah blah def mainTempLoop(self): self.logFileLock.acquire() blah blah, do stuff, read log file, etc self.logFileLock.release() time.sleep(30) def checkWebStatsLoop(self): self.logFileLock.acquire() Read/write from the log file, etc self.logFileLock.release() time.sleep(1) if __name__ == "__main__": myRoomHandler = RoomHandler() myRoomThread = threading.Thread(target=myRoomHandler.mainTempLoop) myRoomThread.start() # Temperature loop function starts running w/ 30 second intervals myRoomHandler.checkWebStatsLoop() # Web handler starts running in main thread # At this point both threads should be running indefinitely
What version of python are you using? - I think I'm using ver 2.7 (how do I check?)
Is your second program, the one that continuously checks for data updates/requests, in a continuous loop? - The second Python program uses FLASK to capture the data from my web page (accessed by phone), its triggered to record the data from the log file when the web page accesses the Root URL of the Pi. I guess FLASK does this by monitoring continuously; but I certainly don't have a Python loop in it.
Lastly, do you have these programs starting automatically with the pi and if so, how are you currently doing that? - No I don't have them starting automatically at the moment, but that is the target, getting them to both run together from a single start up command in the boot file
Thanks, this solution works great and its running fine right now! (not done the boot file changes to make it all auto run at boot just yet).AndrewS wrote:It's not quite what you're asking for, but an even easier solution might just be to run the first script in the background?
If the file isn't too big, and you don't need it if the pi depowered, save it in /run/shm then it won't be using the flash card.vince31 wrote: ↑Wed Sep 17, 2014 6:32 pmThanks, this solution works great and its running fine right now! (not done the boot file changes to make it all auto run at boot just yet).AndrewS wrote:It's not quite what you're asking for, but an even easier solution might just be to run the first script in the background?
I guess there is a much better way of doing things though as suggested by others above using a single file with threads etc, and I will explore these options further now its up and running (thanks again for the simple fix). I hope in the end I can run both progs/threads without the need for an intermediate data file (with its constant reading and writing to the flash card) by sharing the variables between the 2 programs/threads.