'python watchdog stops capturing events after a few mins
I'm running a python script to monitor and log any file changes in a specific folder using watchdog. The script runs for about 12 hours but after around 15 mins of running and logging the file changes, the script stops capturing any events even though there are new files being dropped in the folder.
I did check and the python script is still running in the background.
Can anyone advise why?
import os
import logging
import time
import datetime
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
#Functions for event handler
def on_created(event):
#Checking file size
file_size = -1
while file_size != os.path.getsize(event.src_path):
file_size = os.path.getsize(event.src_path)
time.sleep(5)
logging.info("File_created=" + event.src_path + "; file_size=" + str(file_size))
def on_modified(event):
logging.info("File_modified=" + event.src_path + "; file_size=" + str(file_size))
def on_deleted(event):
logging.info("File_deleted=" + event.src_path)
def on_moved(event):
logging.info("File_moved=" + event.src_path)
def file_checker():
logdate = datetime.datetime.now().strftime('%Y%m%d')
logging.basicConfig(level=logging.INFO, filename='file_monitor_'+logdate+'.log', format='%(asctime)s %(levelname)s : %(message)s',datefmt='%Y-%m-%d %H:%M:%S')
#Set folder to monitor
path = '/data/files/current'
# Initialize Filesystem event handler
event_handler = FileSystemEventHandler()
# Call the event handler functions
event_handler.on_created = on_created
#event_handler.on_modified = on_modified
event_handler.on_deleted = on_deleted
event_handler.on_moved = on_moved
# Initialize Observer
observer = Observer()
observer.schedule(event_handler, path, recursive=False)
# Start the observer
observer.start()
try:
logging.info("Monitoring started")
while True:
# Set the thread sleep time
time.sleep(2)
except KeyboardInterrupt:
observer.stop()
logging.info("Monitoring stopped")
observer.join()
if __name__ == '__main__':
file_checker()
Solution 1:[1]
It turns out I was immediately returning a promise and had to set up an async function to handle the request.
const { isLoading, error, data } = useQuery("repoData",
async () => {
const data = await fetch("https://url_here")
.then(function(response) {
return response.json()
}
)
setData(data)
}
For context, this request is wrapped in react-query but works without it as well.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | epgamer |
