Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.I have the following function:
Python
from threading import Timer
a = 0
def TaskManager():
    global a
    t = Timer( 1, TaskManager )
    t.start()
    if(a == 5):
        t.cancel()
        return
    print("hello")
    a += 1
    

TaskManager()


I don't want to use time.sleep.
So, I want to pass a parameter in TaskManager function ,fox example I want to pass number 5, in order to wait for 5 seconds.
I tried to do this one:

What I have tried:

Python
from threading import Timer
def TaskManager(delay_time):
    
    t = Timer( 1 , TaskManager [delay_time])
    t.start()
    if(delay_time == 0):
        t.cancel()
        return
    print("hello")
    delay_time -= 1
    

TaskManager(5)


in order to delay for 5 sec. but I do not want interruption (like time.sleep() ).
I understand why the code is not working(because recursion never finishes)

I want something like millis() function from arduino( if you know).
How can I do this?
Thanks
Posted
Comments
Nelek 17-Feb-21 16:43pm    
Has to be done as a recursion? Can't be done as a loop?
Nick_is_asking 17-Feb-21 16:55pm    
But,Timer() method has to pass a function parameter.
Nick_is_asking 17-Feb-21 17:29pm    
Any ideas?
Richard MacCutchan 18-Feb-21 4:28am    
That code is confusing because every time the Timer pops it will the TaskManager function, and keep going. In fact, when I tried it Python killed the job (quite correctly) with "RecursionError: maximum recursion depth exceeded".

Maybe if you explained exactly what problem you are trying to solve ...
Nick_is_asking 18-Feb-21 4:35am    
I want to do something for a few seconds(for example) without interrupt the whole code like time.sleep().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900