0

I am trying to run this script in the background of a flask web app. This is an example code of what I am trying to do without the PIR sensor connected. I am essentially running this infinite loop and would like to write to a file periodically within a thread. I do not understand what is wrong and why the file is empty.

import threading
from datetime import datetime as dt
from time import sleep

global_lock = threading.Lock()

def write_to_file():
    while global_lock.locked():
        continue


def motionlog():
    global_lock.acquire()
    f = open("motionlog.txt", mode = "w")
    f.write("Motion Detection Log" + "\n")
    while True:
            #output_lock.acquire()
                f.write("Motion Detected at "+ dt.now().strftime("%m_%d_%Y-%I:%M:%S_%p")+"\n")
                print('Motion Detected')
                sleep(5)
    global_lock.release()
t1 = threading.Thread(target=motionlog)
t1.start()
t1.join()
Freehalo3
  • 1
  • 1
  • Will other thread also access this file? If not, no need for any locks, just write to it like you would without threads. – Some programmer dude May 09 '22 at 19:15
  • As for your problem, if you just *call* the function `motionlog` (without using any threads), what happen then? Have you tried to debug it without threads? – Some programmer dude May 09 '22 at 19:16
  • And talking about locks, the thread function tries to acquire the lock, and won't release it until it ends (which it never does). – Some programmer dude May 09 '22 at 19:17
  • you need to close the file to see the changes by calling f.close() or using a context manager: `with open("motionlog.txt", "w") as f:` – Henry D May 09 '22 at 19:20
  • @Some programmer dude I am not writing to this file with another thread, but the contents of this file will be displayed on an html page. it works just fine without a thread, but I need threading to run this in the background while my main program runs a flask application – Freehalo3 May 10 '22 at 20:06
  • @Henry D In the function where would I close the file? I wanted this to continuously run in the background and have the main thread run a flask app. – Freehalo3 May 10 '22 at 20:11
  • Then you don't need any locks. And I doubt you really need a lock at all, if you're just appending to the file then whenever you get an event that you need to append to the file just open it in append-mode, write the line, and close the file. It's extremely quick, and file-size doesn't matter for this operation. – Some programmer dude May 11 '22 at 05:29
  • @Some programmer dude That worked. Thanks for the help! – Freehalo3 May 11 '22 at 13:52
  • My previous comment came out a little wrong. I meant to say that I don't think you need *threads* at all. – Some programmer dude May 11 '22 at 16:35
  • @Freehalo3 take a look at this: https://stackoverflow.com/questions/19756329/can-i-save-a-text-file-in-python-without-closing-it using `f.flush()` followed by `os.fsync(f.fileno())` Still, be sure to close the file when you are done logging. – Henry D May 11 '22 at 16:47

0 Answers0