Home > database >  How to use Threading timers in a while loop
How to use Threading timers in a while loop

Time:03-22

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()

CodePudding user response:

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()

CodePudding user response:

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 something reason regardless of what I do I am not using I don't want to use sleep() since it would delay the loop but the I am starting to think it might be too hard even though 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 so 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()
  • Related