Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 13-04-2009
Member
 
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,
Reply With Quote
  #2  
Old 13-04-2009
Member
 
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.
Reply With Quote
  #3  
Old 13-04-2009
Member
 
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...

Quote:
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.
Reply With Quote
  #4  
Old 13-04-2009
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 11:35 AM.