Python Forum
python delay without interrupt the whole code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python delay without interrupt the whole code
#1
Hello.I have the following function:
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:
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 to do something for a few seconds(for example) without interrupt the whole code like time.sleep().
I want something like millis() function from arduino( if you know).
How can I do this?
Thanks
Reply
#2
It is very difficult to understand the purpose of the TaskManager() function. Obviously you want the program to print 'hello' now and then, but not too fast, with delays. Can you describe without code what you expect the program to do?
buran likes this post
Reply
#3
Let us know if this is what you are looking for:

from threading import Timer

breaker_breaker = False

def delayed_function (seconds_delayed) :
	global breaker_breaker
	breaker_breaker = True
	print (f'This funciton was delayed for {seconds_delayed} seconds.')

wait_for_it = Timer (3, delayed_function, '3')
wait_for_it.start ()

while breaker_breaker == False:
	print ('Still waiting...')
	for dummy in range (999999) : dummy =+ 1
Reply
#4
Yes, this is what I want.Thank you very much!
Reply
#5
This sort of solution would likely not work in a lot of other languages. Most compilers would be smart enough to realize that the for-loop body is constant (it always sets "dummy" to a constant +1), and also that there's no side effects, and would thus delete the loop entirely from the resulting executable.

I'd be a little worried, myself, that an infinite loop without any sort of time.sleep() would be very cruel to the processor. Even a tiny one, like time.sleep(0.0001) would be better than none at all.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to add a delay right after a request.get() cubangt 6 2,779 Sep-07-2023, 09:29 AM
Last Post: shoesinquiry
  add interrupt for next task kucingkembar 0 777 Oct-07-2022, 12:15 PM
Last Post: kucingkembar
  Determine if keyboard interrupt versus SIGINT trapped? Jibunnokage 5 1,802 Sep-30-2022, 06:54 AM
Last Post: Gribouillis
  Request Delay pheadrus 1 3,810 Nov-25-2021, 08:51 PM
Last Post: snippsat
  adding a delay on end Daz2264 6 2,488 Sep-29-2021, 02:57 PM
Last Post: deanhystad
  Enabling interrupt on Adafruits button/led board Moris526 0 2,026 Apr-30-2021, 03:29 PM
Last Post: Moris526
  Adafruits Neotrellis interrupt with RAsp and Python Moris526 5 3,582 Jan-01-2021, 11:43 PM
Last Post: Moris526
  Interrupt for Adafruits Neotrellis button/led board Moris526 0 1,808 Dec-28-2020, 05:42 AM
Last Post: Moris526
  input interrupt Nickd12 1 4,276 Dec-09-2020, 05:01 PM
Last Post: Gribouillis
  How to read CSV file one row at the time in a range and some delay in between greenpine 2 4,756 Nov-20-2020, 02:26 PM
Last Post: greenpine

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020