'writing to text file within thread python

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()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source