I need to run a specific function every second. I have searched for a solution and found this one:
import threading
def printit():
threading.Timer(5.0, printit).start()
print "Hello, World!"
printit()
It seems to be the code I have been searching for. But after running those lines of code for 20 Minutes I have got following error message:
Exception in thread Thread-1896: Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner File "/usr/lib/python3.5/threading.py", line 1180, in run File "/home/pi/scanner/api/brightness.py", line 37, in adjust_brightness File "/home/pi/scanner/api/brightness.py", line 14, in get_brightness OSError: [Errno 24] Too many open files
It seems that every time the function is called a new thread starts. Is there a way to stop the previous thread before the new one gets generated? Or are there other solutions where this doesn't happen?