1

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?

Saeid
  • 4,147
  • 7
  • 27
  • 43
anton68
  • 387
  • 3
  • 14
  • Exactly what I thought. How can my problem be better solved? – anton68 Sep 15 '18 at 08:17
  • You said "every time the function is called a new thread starts". Indeed! It's a simple auto-repeating timer. A program that uses it is only supposed to call it once, and then it restarts itself every 5 seconds. So you will cause chaos if you try to call it yourself multiple times. Maybe take a look at some of the other solutions on that page, like MestreLion's. – PM 2Ring Sep 15 '18 at 09:07

0 Answers0