I want to define an object that contains an id, status and countdown timer. As soon as I instantiate this object, the countdown timer should set off immediately.
Below is the code where I create a class that has a non threaded version of the countdown timer defined in it
import time
import sys
from threading import Timer
class UserTimer:
def __init__(self, id=None, current_status=None):
self.id = id
self.current_status = current_status
self.timeout()
def timeout(self):
print("timeout started for", self.id)
timeout_limit = 150
seconds = 0
while True:
try:
if seconds == timeout_limit:
print("countdown over for", self.id)
break
time.sleep(1)
seconds += 1
except KeyboardInterrupt, e:
break
Below is how an instantiate it
params1 = {'id': "test@hello.com", 'current_status': "success"}
params2 = {'id': "test2@hello.com", 'current_status': "success"}
user1 = UserTimer(**params1)
user2 = UserTimer(**params2)
The problem here is that when this program runs, it will instantiate the first object (user1) and due to the function time.sleep(), it will wait for the given duration before instantiating the second object (user2)
So I then looked up and found python threads helpful in this situation since the threads will run independent and won't block the progression of the code.
So this is how I changed the code below
class UserTimer:
def __init__(self, id=None, current_status=None):
self.id = id
self.current_status = current_status
def timeout(self):
print("time over for", self.id)
t = Timer(150, timeout)
t.start()
params1 = {'id': "test@hello.com", 'current_status': "success"}
params2 = {'id': "test2@hello.com", 'current_status': "success"}
user1 = UserTimer(**params1)
user2 = UserTimer(**params2)
Now both the objects get instantiated simultaneously but the problem is as soon as the given duration is over, it gives the following error
Exception in thread Thread-1:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 1073, in run
self.function(*self.args, **self.kwargs)
TypeError: timeout() takes exactly 1 argument (0 given)
This is because it sees the keyword self which it doesn't expect. But I need the self since I need to dump some information about the user object. If I remove self, then it runs fine.
Am I defining threads inside a class in a wrong manner? What I want is to be able to instantiate multiple objects that have their own countdown timers.
Additionally I should also be able to reset the countdown timer by doing something like user1.reset_timer = True
What am I doing wrong here?