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
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
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 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
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417