I've been reading up on threading and tried implementing it into my code however I'm not sure if the way I'm doing it, is the best practise.
My code simply imports a self scripted package which pulls weather data and runs the package every 60 seconds thereafter.
I plan on running multiple packages which gather data at once, when I have worked out a good code technique.
from package.weather import weatherapi
This is a really bad use of
Threading.timer. You're constantly starting new threads, when you just want one thread to do something regularly. This code is equivalent:
from package.weather import weatherapi import threading import time def update(): while True: weatherapi() time.sleep(60) WHEATHER_THREAD=threading.Thread(target=update) WHEATHER_THREAD.daemon = True # main can exit while thread is still running WHEATHER_THREAD.start()
Since threads all use the same namespace, you can also make do with just one function.
UPDATE_CALLABLES = [weatherapi] # add new functions to have them called by update def update(): while True: for func in UPDATE_CALLABLES: func() time.sleep(60)
UPDATE_CALLABLES can also be appended while the Thread is already running.