Results 1 to 4 of 4

Thread: How to Interrupt Sleep Function in Python

  1. #1
    Join Date
    Apr 2009
    Posts
    68

    How to Interrupt Sleep Function in Python

    Hello programming gurus,

    I'm looking for a function in the standard library or pywin32 package that will block until a certain condition is met or it is interrupted by Ctrl-C. and for that i have decided to write sleep function to satisfy the condition,so can anyone suggest me how to write the sleep function using python.

    Regards,

  2. #2
    Join Date
    Jan 2009
    Posts
    143

    Re: How to Interrupt Sleep Function in Python

    To write a sleep function in python we more generally use the following convention
    Code:
    findertools.sleep()
    Tell the finder to put the Macintosh to sleep, if your machine supports it.

    You can hook the it on the main thread and just pass it. And let the threads execute.

  3. #3
    Join Date
    Jan 2006
    Posts
    211

    Re: How to Interrupt Sleep Function in Python

    Well you've certainly picked a ticklish area to run into problems. First, forget about the threading aspects for the moment. AFAICT the smallest program which reproduces your problem is:

    Code:
    import signal
    import time
    
    def handler (*args):
    pass
          signal.signal(signal.SIGBREAK, handler)
           time.sleep (10)
    Now run that and do a ctrl-break somewhere in that time.sleep. Sure enough...

    Traceback (most recent call last):
    File "C:\data\temp\sig3.py", line 8, in <module>
    time.sleep (10)
    IOError: [Errno 4] Interrupted function call

    Under the covers, the sleep function is implemented as a WaitForSingleObject call with a timeout. The object being waited on is an anonymous event which is set from a console ctrl handler when one of those interrupts happens (ctrl-c / ctrl-break / shutdown). This makes sure that the (otherwise blocking) sleep can be interrupted.

  4. #4
    Join Date
    Jan 2009
    Posts
    140

    Re: How to Interrupt Sleep Function in Python

    This function suspends execution for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time.

    The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal's catching routine.

    Code:
    time.sleep(t)
    Here is the detail of parameters:

    • t: This is the number of seconds execution to be suspended.


    This produces following result:
    Start : Tue Mar 17 10:19:18 2009
    End : Tue Mar 17 10:19:23 2009

Similar Threads

  1. Sleep function for Logitech Harmony 525
    By Devabrata in forum Hardware Peripherals
    Replies: 6
    Last Post: 10-06-2010, 06:39 AM
  2. thread,time and sleep in Python
    By Brunoz in forum Software Development
    Replies: 5
    Last Post: 08-03-2010, 08:13 PM
  3. sleep() function in C
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 03-03-2010, 06:53 PM
  4. How to write pause, sleep, wait function
    By VinFanatic in forum Software Development
    Replies: 3
    Last Post: 07-07-2009, 03:36 PM
  5. JavaScript Sleep Function
    By DARIELLE in forum Software Development
    Replies: 3
    Last Post: 29-06-2009, 09:30 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,975,677.28645 seconds with 17 queries