Python Forum
Can you end the Time.sleep function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Can you end the Time.sleep function (/thread-32035.html)



Can you end the Time.sleep function - boier96 - Jan-16-2021

Hey im working on a python project and i got an idea to use time.sleep a few times. After a while i started wondering how to end the time.sleep function before all the time had gone by. So that is my question, Can (If so then how?) you make the function end before the time is out?


RE: Can you end the Time.sleep function - deanhystad - Jan-16-2021

Assume there is a function time.wakeup() that terminates time.sleep(). How are you going to call time.wakeup()?


RE: Can you end the Time.sleep function - Larz60+ - Jan-16-2021

split your sleep time into very small increments, this will allow time for interrupting the sleep process.
There's an example here: https://stackoverflow.com/a/23570155

you could also use time.perf_counter see: https://docs.python.org/3/library/time.html#time.perf_counter


RE: Can you end the Time.sleep function - deanhystad - Jan-16-2021

Maybe you need to think about why you are using sleep? I don't know if I have ever used time.sleep() in a program. What are you doing that needs to be done slowly?


RE: Can you end the Time.sleep function - snippsat - Jan-16-2021

Usually if i guess right so is better for this task to look into something like schedule, APScheduler
Something a posted before,can start different task at long or short interval.
import schedule
import time, os
import subprocess
 
def make_file():
    fn = 'test.out'
    open(fn, 'a').close()
    print(f'Making <{fn}>')
 
def del_file():
    try:
        fn ='test.out'
        os.remove(fn)
        print(f"Removed <{fn}>")
    except FileNotFoundError:
        print(f'Could not find <{fn}>')
 
def run_sh():
    '''Example running shell script'''
    subprocess.run(['bash', 'start.sh'])
 
schedule.every(8).seconds.do(make_file)
schedule.every(12).seconds.do(del_file)
while True:
    schedule.run_pending()
    time.sleep(1)
Output:
λ python remove_file.py Making <test.out> Removed <test.out> Making <test.out> Removed <test.out> Making <test.out> Making <test.out> Removed <test.out> Making <test.out>
So this is none blocking solution with Popen(as run can run stuff in Parallel).
Remember that time.sleep() is a blocking operation.
APScheduler BackgroundScheduler dos separate (multi)thread automatic.
Or schedule execute jobs in parallel?.


RE: Can you end the Time.sleep function - boier96 - Jan-16-2021

(Jan-16-2021, 05:47 PM)Larz60+ Wrote: split your sleep time into very small increments, this will allow time for interrupting the sleep process.
There's an example here: https://stackoverflow.com/a/23570155

you could also use time.perf_counter see: https://docs.python.org/3/library/time.html#time.perf_counter

Thank you i will try this


RE: Can you end the Time.sleep function - boier96 - Jan-16-2021

(Jan-16-2021, 06:53 PM)deanhystad Wrote: Maybe you need to think about why you are using sleep? I don't know if I have ever used time.sleep() in a program. What are you doing that needs to be done slowly?

I'm currently making a program that is able to respond to different commands and one such command is wait. But i would also like to be able to give a command to stop the pause while it is still counting down. So its not rly about doing it slowly.


RE: Can you end the Time.sleep function - boier96 - Jan-16-2021

(Jan-16-2021, 05:43 PM)deanhystad Wrote: Assume there is a function time.wakeup() that terminates time.sleep(). How are you going to call time.wakeup()?

By voice.


RE: Can you end the Time.sleep function - deanhystad - Jan-16-2021

If you are sleeping, are you still listening?


RE: Can you end the Time.sleep function - Serafim - Jan-16-2021

Before Python 3.5 it was actually possible to "wake up" a time.sleep by sending some interrupt signal to the running program but from python 3.5 and up this is no longer possible. The only possibility is to send an interrupt signal that generates an exception. You will have to know how to generate interrupt signals and how to make these signal handlers cause an exception. Never tried it myself but I can imagine that you can make some keyboard combination generate an interrupt signal which you handle by a custom signal handler that triggers an exception.
I also think it is an interesting problem as some signals, like ctrl-c, are meant to immediately interrupt a running program. Most GUI programs have some kind of loop to handle events and if you press ctrl-c it may take a considerable time before the program reacts, if it does at all react.
But in the comments to time.sleep it is (or was a the time I read it, it might have changed again) explicitly stated that time.sleep(n) will sleep for at least n seconds even if there is an interrupt signal unless the signal causes an exception.