Python Forum

Full Version: Can you end the Time.sleep function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
Assume there is a function time.wakeup() that terminates time.sleep(). How are you going to call time.wakeup()?
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.h...rf_counter
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?
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?.
(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.h...rf_counter

Thank you i will try this
(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.
(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.
If you are sleeping, are you still listening?
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.