Saturday, 24 August 2013

Python class instance starts method in new thread

Python class instance starts method in new thread

I spent the last hour(s???) looking/googling for a way to have a class
start one of its methods in a new thread as soon as it is instanciated.
I could run something like this:
x = myClass()
def updater():
while True:
x.update()
sleep(0.01)
update_thread = Thread(target=updater)
update_thread.daemon = True
update_thread.start()
A more elegant way would be to have the class doing it in init when it is
instanciated. Imagine having 10 instances of that class... Until now I
couldn't find a (working) solution for this problem... The actual class is
a timer and the method is an update method that updates all the counter's
variables. As this class also has to run functions at a given time it is
important that the time updates won't be blocked by the main thread.
Any help is much appreciated. Thx in advance...

No comments:

Post a Comment