need help about time.sleep, timer

Discussion in 'Python' started by Sinan Nalkaya, Nov 18, 2005.

  1. 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.
    Sinan Nalkaya, Nov 18, 2005
    #1
    1. Advertising

  2. Sinan Nalkaya

    Peter Hansen Guest

    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
    Peter Hansen, Nov 19, 2005
    #2
    1. Advertising

  3. On Fri, 18 Nov 2005 22:45:37 -0500, Peter Hansen <>
    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)



    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Nov 19, 2005
    #3
  4. Dennis Lee Bieber wrote:

    >On Fri, 18 Nov 2005 22:45:37 -0500, Peter Hansen <>
    >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.
    Sinan Nalkaya, Nov 21, 2005
    #4
  5. Sinan Nalkaya

    Steve Holden Guest

    Sinan Nalkaya wrote:
    > Dennis Lee Bieber wrote:
    >
    >
    >>On Fri, 18 Nov 2005 22:45:37 -0500, Peter Hansen <>
    >>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/
    Steve Holden, Nov 21, 2005
    #5
  6. On Mon, 21 Nov 2005 10:35:20 +0200, Sinan Nalkaya <> wrote:

    >Dennis Lee Bieber wrote:
    >
    >>On Fri, 18 Nov 2005 22:45:37 -0500, Peter Hansen <>
    >>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
    Bengt Richter, Nov 21, 2005
    #6
  7. Bengt Richter wrote:

    >On Mon, 21 Nov 2005 10:35:20 +0200, Sinan Nalkaya <> wrote:
    >
    >
    >
    >>Dennis Lee Bieber wrote:
    >>
    >>
    >>
    >>>On Fri, 18 Nov 2005 22:45:37 -0500, Peter Hansen <>
    >>>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.
    Sinan Nalkaya, Nov 22, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kelsang Wangchuk

    System.Timers.Timer vs. System.Threading.Timer

    Kelsang Wangchuk, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    704
    Kelsang Wangchuk
    Jul 31, 2003
  2. Gonzalo Moreno
    Replies:
    2
    Views:
    12,378
  3. JohnnyFive

    Sleep timer but still responsive?

    JohnnyFive, Jan 28, 2010, in forum: Python
    Replies:
    7
    Views:
    396
    Dave Angel
    Jan 30, 2010
  4. Sam Roberts
    Replies:
    16
    Views:
    364
    Daniel Berger
    Feb 18, 2005
  5. Richard
    Replies:
    7
    Views:
    215
    Richard
    May 22, 2007
Loading...

Share This Page