954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

time.sleep()

I'm frustrated by the nature of time.sleep(), which prevents the script from doing anything else during the allotted time period. For example, I have:

while True:
    checkSomething()
    time.sleep(0.1)

print 'hello'


In this case, "hello" is never printed. I really want to check something every few milliseconds, but I also need my program to be doing other things! Please help.

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 

You will want to use the threading features of python.
Basically the 'while' will be the default thread running and the print another thread that you previously created with a python module, threading.
Hey, there is also a timer class in this module: http://docs.python.org/lib/timer-objects.html
Sorry, I am a bit rusty on threading but if you google "python thread" you will find your way and anyway http://docs.python.org/lib/module-threading.html
Have fun!

demetrio
Newbie Poster
4 posts since Jun 2007
Reputation Points: 10
Solved Threads: 2
 

Not to be overly pedantic, but in your example the problem is not in time.sleep() but rather the while True that never exits the loop. Your routine checkSomething() is getting called every 0.1 seconds, but not saying the loop is over.

Per demetrio, threading is probably what you need. Or, if suitable, you could also write code as ff:

while True:
    if checkSomething():
         break
    time.sleep(0.1)

print 'hello'

... of course,checkSomething() will need to return True when it's time to quit, False otherwise.

BearofNH
Posting Whiz
323 posts since May 2007
Reputation Points: 94
Solved Threads: 48
 

Ah ha, but I want it to continue to check the something throughout the program -- it should never stop checking! Meanwhile it should print 'hello' just after it begins to check.

Basically what is happening is the user is listening to a series of tones and pressing a button in synchrony with them. The while loop is designed to "listen" for these button presses (and record when they occur), which continue throughout the program.

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 
Per demetrio, threading is probably what you need. Or, if suitable,

I third that statement. Time to look at some tutorials as to how to work with threads!

ffao
Newbie Poster
15 posts since May 2007
Reputation Points: 20
Solved Threads: 5
 

G'day,

My understanding of threads in Python is that only one thread at one time can perfom a particular job. Python does not support multiple threads, unlike Java etc.

If you were to do one thread at a time you would need to put a Global Lock on each performing thread and then after each thread has done it's job you then have to unlock that thread so another thread can take over. There is also another function the Queue() function which places each thread in line one after the other. But from what I can gather about your post, is that you want two independant threads working simultaneously which is impossible with Python because as I've said Python does not support multiple threading simultaneously.

I've worked on a simular problem to yours so I investigated threads but found this limiting factor (only one thread working at one time) halted all progress. There may be another way around this problem some type of work around, so don't give up hope yet.

Hope this saves you some valuable time.:)

fredzik.

fredzik
Junior Poster in Training
55 posts since Feb 2007
Reputation Points: 10
Solved Threads: 5
 

I'm frustrated by the nature of time.sleep(), which prevents the script from doing anything else during the allotted time period. For example, I have:

while True:
    checkSomething()
    time.sleep(0.1)

print 'hello'

In this case, "hello" is never printed. I really want to check something every few milliseconds, but I also need my program to be doing other things! Please help.


Hi, it is clear that you have to go with threading. Here is what I think you should do..

import threading

class backgroundTask(threading.Thread):
	def __init__(self):
		# Do initialization what you have to do
		self.check=True
	def run(self):
		if self.check  and checkSomething():
			break
		time.sleep(0.1)

bg = backgroundTask()
bg.start()

print 'hello! checkSomething() is running in the background'
#  do some more operations...
#....
bg.join()# you wait for background task to finish.

print 'The End...'

Note: you can now return True when you want to quit background task. False otherwise.

Hope it helps.

kath.

katharnakh
Posting Whiz in Training
237 posts since Jan 2006
Reputation Points: 19
Solved Threads: 34
 

I just wanted to write to say thank you to everybody who told me to look up threading... I have finally gotten around to it, and it solves my problem nicely!

You're all great!

aot
Junior Poster in Training
83 posts since Feb 2007
Reputation Points: 10
Solved Threads: 1
 

i would say somthing in style with this:

while True:
checkSomething()
time.sleep(0.1)
print 'hello'

and then it will print:
hello
hello
hello
and so on...

it works for me

irnsu44
Newbie Poster
1 post since Jun 2011
Reputation Points: 10
Solved Threads: 1
 

You might want to check the example of a simple threading decorator here:
http://www.daniweb.com/software-development/python/threads/20774/1576229#post1576229

It will show you that time.sleep() can run in the background.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Take a look at Twisted; it takes the pain and wonder out of threads: http://twistedmatrix.com/documents/current/core/howto/time.html

txfoo
Newbie Poster
1 post since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You