need help about time.sleep, timer

Steve Holden steve at holdenweb.com
Mon Nov 21 09:34:40 EST 2005


Sinan Nalkaya wrote:
> Dennis Lee Bieber wrote:
> 
> 
>>On Fri, 18 Nov 2005 22:45:37 -0500, Peter Hansen <peter at engcorp.com>
>>declaimed the following in comp.lang.python:
>>
>> 
>>
>>
>>>It's quite unclear whether the last part, above, is one of your 
>>>*requirements*, or a description of a problem you are having with your 
>>>current approach.  Do you *want* it to wait forever if you don't enter 
>>>anthing?
>>>
>>>   
>>>
>>
>>	As I understand it, he (?) wants to accumulate characters to be
>>passed to a certain function -- but the function is not to be invoked
>>until after a time period has expired; the time period resetting on each
>>character entered.
>>
>>	Something I'd do with threads, queues, and sleep...
>>
>>PSEUDOCODE
>>
>>thread1:
>>	while not Shutdown:
>>		ch = getChar()
>>		q.put(ch)
>>
>>
>>thread2: #or main
>>	while not Shutdown:
>>		chars = []
>>		while True:
>>			sleep(max_interval)
>>			if q.empty(): break #no input since start of sleep
>>			while not q.empty():	#collect all input from sleep
>>				chars.append(q.get())
>>		inp = "".join(chars)
>>		function(inp)
>>
>>
>>
>> 
>>
> 
> i appreciate your comments and ideas. Dennis told exactly what i tried 
> to say :), code seems to be fine but during sleep action i think the 
> above code does not execute
> 
> if q.empty(): break #no input since start of sleep
> 			while not q.empty():
> 				chars.append(q.get())
> 
> i need something, while sleeping, executes the function that waits for 
> input from keyboard.
> i imagined something like that, i have a queu, it both stores the keys 
> that pressed and pressing times of these keys, then i`ll proccess these 
> times. here is scenario
> input : 5
> after 10 seconds later input 5 is being proccessed
> return back to main function
> input : 1
> after 5 seconds , other input 5
> after 5 more seconds , 15 is being proccessed
> Thanks.

It sounds like you will have to learn how to do two things in parallel, 
which is normally done using threads. Unfortunately it also seems you 
would like the input thread to be terminated when the timer thread finds 
its time is up, and that's not particularly easy because there's no way 
to stop a thread "from the outside" once it's started.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list