473,372 Members | 1,197 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

need help about time.sleep, timer

hello,
i need a function like that,
wait 5 seconds:
(during wait) do the function
but function waits for keyboard input so if you dont enter any it waits
forever.
i tried time.sleep() but when i used time.sleep in while, the function
in while never executed.
then i found something about Timer but couldnt realize any algorithm in
my mind.
i`ll use the function like remote controller. as you know in remote
controllers, tv waits you for second number input. if you dont press
any, executes the first number that you have entered.
thanks.
Nov 22 '05 #1
5 3079
Sinan Nalkaya wrote:
i need a function like that,
wait 5 seconds:
(during wait) do the function
but function waits for keyboard input so if you dont enter any it waits
forever.


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?

-Peter
Nov 22 '05 #2
Sinan Nalkaya wrote:
i need a function like that,
wait 5 seconds:
(during wait) do the function
but function waits for keyboard input so if you dont enter any it waits
forever.


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?

-Peter
Nov 22 '05 #3
Dennis Lee Bieber wrote:
On Fri, 18 Nov 2005 22:45:37 -0500, Peter Hansen <pe***@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.
Nov 22 '05 #4
Dennis Lee Bieber wrote:
On Fri, 18 Nov 2005 22:45:37 -0500, Peter Hansen <pe***@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.
Nov 22 '05 #5
Bengt Richter wrote:
On Mon, 21 Nov 2005 10:35:20 +0200, Sinan Nalkaya <er*************@gmail.com> wrote:
Dennis Lee Bieber wrote:
On Fri, 18 Nov 2005 22:45:37 -0500, Peter Hansen <pe***@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.


If I understand, you really don't want to sleep, you want to wait a max time of 5 seconds
for a character. You can get that from a queue.get(True,5) hence, you could try the
following as a start (untested, and I don't have much experience with python threading,
so will wait for bug reports ;-)

Note that it uses getch for input, which doesn't echo. (Change to getche if you want echo)
You can run this from the command line with one arg: the number of seconds you want to
have the test continue. At the end of that time it will set the Shutdown event, and
thread2 should empty the queue and wait 5 seconds and then do its last function call and
see the shutdown event.

----< tqinp.py >-----------------------------------------------------------------------
# tqinp.py -- test queued input of unechoed character input until a 5-second delay
import threading
import Queue
import msvcrt
queue = Queue.Queue(10) # 2 is prob enough for this thing
Shutdown = threading.Event()

def getChar(): return msvcrt.getch() # blocks
def function(s): print 'function(%r)'%s

def thread1(q):
while not Shutdown.isSet():
ch = getChar()
q.put(ch)

def thread2(q): #or main
while not Shutdown.isSet():
chars = ''
try:
while True: chars += q.get(True, 5)
except Queue.Empty, e:
print 'No input for 5 seconds. Using %r'%chars
function(chars)

import time
def test(trial_time):
thr1 = threading.Thread(target=thread1, args=(queue,))
thr1.start()
thr2 = threading.Thread(target=thread2, args=(queue,))
thr2.start()
t0 = time.clock()
time.sleep(trial_time)
print 'Ending trial after %s seconds' % (time.clock()-t0)
Shutdown.set()

if __name__ == '__main__':
import sys
if not sys.argv[1:]: raise SystemExit,'Usage: python tqinp.py'
test(float(sys.argv[1]))
---------------------------------------------------------------------------------------

Ok, in the following I'll type 123 <pause 5> abc <pause to let it die>

[ 5:40] C:\pywk\clp\threadstuff>py24 tqinp.py 15
No input for 5 seconds. Using '123'
function('123')
No input for 5 seconds. Using 'abc'
function('abc')
Ending trial after 14.9948775627 seconds
No input for 5 seconds. Using ''
function('')

[ 5:41] C:\pywk\clp\threadstuff>

Except it didn't die until I hit another key to let thread1 loop and see its Shutdown test.
But this is alpha 0.01, so you can fix that various ways. Or maybe get the real requirements down ;-)
HTH
(once I get it posted -- news reads ok now but news server is not accepting posts.
I suspect they do system-hogging chores Sun night wee hours ;-/

Regards,
Bengt Richter

thanks for all replies especially via with code ones ;) steve`s codes
works perfectly except time part (ill look investigate it), i had to do
little edit because i`m unix user and dont have msvcrt but no problem.
also i am noticed that, i have to study on threading much as you said.
thanks again.
Nov 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
10
by: WhiteSocksGuy | last post by:
Help! I am new to Visual Basic .Net (version 2002) and I am trying to get a System.Timers.Timer to work for me to display a splash screen for about two seconds and then load the main form. I have...
18
by: Max | last post by:
This is a follow-up on my previous thread concerning having the program wait for a certain date and time and then executing some code when it gets there. My question is; can I use the Sleep...
4
by: Manuel | last post by:
I have a long function that needs to be done 1000 times. I'm multithreading it, but I don't want to load them up all at once, instead load them 10 at a time. So far the only way I can get it to...
5
by: BobAchgill | last post by:
I have a Form that on a timer uses FTP to update data on the local disk. I would like this Form to start on boot of the computer and stay running still shutdown. Like putting it in the...
0
by: Sinan Nalkaya | last post by:
hello, i need a function like that, wait 5 seconds: (during wait) do the function but function waits for keyboard input so if you dont enter any it waits forever. i tried time.sleep() but when...
4
by: Lemune | last post by:
Hello everyone. I'm using vb 2005. I'm creating program that run as service on windows. And in my program I need to use timer, so I'm using timer object from component. I try my source code on...
4
by: barker7 | last post by:
In our app, we need to collect data at regular intervals (4, 8 or 16 seconds - user settable). The collection happens in a background thread. My first approach was to do the collection, which takes...
12
by: gagonm | last post by:
HI I am working on Engineering application where we need thread.sleep() function many times. But since sleep interval is not accurate we need an alternative which can mimick functionality of...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.