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.

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!

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.

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.

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!

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.

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.

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!

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.