|Estimated completion time||1m|
Python sleep syntax
The syntax is as follows
The time.sleep() suspend execution of a script 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. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.
Example: Put delay in a Python script
Create a script called demo-sleep.py:
#!/usr/bin/python # The following will run infinity times on screen till user hit CTRL+C # The program will sleep for 1 second before updating date and time again. import time print "*** Hit CTRL+C to stop ***" ## Star loop ## while True: ### Show today's date and time ## print "Current date & time " + time.strftime("%c") #### Delay for 1 seconds #### time.sleep(1)
Save and close the file. Run it as follows:
$ chmod +x demo-sleep.py
- Set an infinite loop using while True:
- Get the current date and time using strftime() and display on screen.
- Finally, add 1 second delay in a script using sleep(1).
- Continue this procedure till user interrupts.
- 30 Cool Open Source Software I Discovered in 2013
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop