'How to use Threading timers in a while loop
What I need help with.
The only reason I used 2 seconds was to delay the messages but I am using a while loop: now since the for loop: did not really work the timer delay does not seem to be working for some reason regardless of what I do. I don't want to use time.sleep() since it would delay the loops in the future I plan to add multiple threads. It seems easy I think the threading module is very bug or at least it does not work the way a person might expect from it but if anyone is experienced with this module it would be a big help. I am sure the answer is simple but I am an idiot that is why I can not figure it out.
from threading import Timer
isint = True
while (isint):
x =input("How many 'somethings' : ")
try:
x =int(x)
isint=False
except ValueError:
print("Type in a number.")
def something():
global x
if x > 0:
print('Something')
x = x-1
else:
pass
while True:
Timer(10 , something).start()
Solution 1:[1]
You should use different value for every Timer
Timer(10, ...)
Timer(12, ...)
Timer(14, ...)
And with loop
for i in range(x):
Timer(10 + i*2, something).start()
and every timer will start 2 seconds later after previous timer without using sleep()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | furas |
