Python Forum
Can you end the Time.sleep function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can you end the Time.sleep function
#1
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?
Reply
#2
Assume there is a function time.wakeup() that terminates time.sleep(). How are you going to call time.wakeup()?
Reply
#3
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
Reply
#4
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?
Reply
#5
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?.
buran likes this post
Reply
#6
(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
Reply
#7
(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.
Reply
#8
(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.
Reply
#9
If you are sleeping, are you still listening?
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  return next item each time a function is executed User3000 19 2,277 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  Use subprocess.Popen and time.sleep chucky831 2 1,944 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  How to immediately kill and restart a thread while using a time.sleep() inside it? philipbergwerf 4 3,521 Feb-07-2022, 04:16 PM
Last Post: Gribouillis
  time function does not work tester_V 4 3,017 Oct-17-2021, 05:48 PM
Last Post: tester_V
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,226 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Time.sleep: stop appending item to the list if time is early quest 0 1,871 Apr-13-2021, 11:44 AM
Last Post: quest
  function call at defined system time? Holon 5 3,222 Oct-06-2020, 03:58 PM
Last Post: snippsat
  Having hard time understanding the function self-returning itself twice jagasrik 2 2,483 Aug-15-2020, 08:50 PM
Last Post: deanhystad
  Writing a function to calculate time range in Unix Epoch Time t4keheart 2 2,996 Jul-15-2020, 01:55 AM
Last Post: t4keheart
  time.sleep mtnwinds 4 2,857 May-21-2020, 10:12 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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