Sleep timer but still responsive?

Discussion in 'Python' started by JohnnyFive, Jan 28, 2010.

  1. JohnnyFive

    JohnnyFive Guest

    I need help with something that is probably fairly simple, but i'm
    having a heck of a time getting it work.

    Basically, I need my program to sleep for a certain amount of time,
    but I don't want the console to become unresponsive while sleeping.

    As soon as the time is up, I want the main program to run it's course
    again.

    I tried using a Timer, threads, etc, but I really can't figure it out.
    What am I missing?

    I can post what I have, but I don't want to get caught up on how i'm
    doing it wrong (as none of it works), but rather the correct way to do
    it.

    Thanks in advance!
    JohnnyFive, Jan 28, 2010
    #1
    1. Advertising

  2. En Thu, 28 Jan 2010 20:34:22 -0300, JohnnyFive <>
    escribió:

    > I need help with something that is probably fairly simple, but i'm
    > having a heck of a time getting it work.
    >
    > Basically, I need my program to sleep for a certain amount of time,
    > but I don't want the console to become unresponsive while sleeping.


    Please provide more details. What do you want your program to do while
    sleeping? What kind of actions do you want a response to?
    Do you have a GUI? A curses-based interfase?

    --
    Gabriel Genellina
    Gabriel Genellina, Jan 29, 2010
    #2
    1. Advertising

  3. JohnnyFive

    JohnnyFive Guest

    On Jan 28, 4:55 pm, "Gabriel Genellina" <>
    wrote:
    > Please provide more details. What do you want your program to do while  
    > sleeping? What kind of actions do you want a response to?
    > Do you have a GUI? A curses-based interfase?
    >
    > --
    > Gabriel Genellina


    My app is purely console based. I just don't want the console to lock
    up (on Windows using time.sleep(x) causes the console to become
    unresponsive until the timer is done), and I want people to be able to
    CTRL+C to stop the script if need be (which can't be done if it's
    unresponsive!).

    Thanks.
    JohnnyFive, Jan 29, 2010
    #3
  4. JohnnyFive

    Andreas Tawn Guest

    > On Jan 28, 4:55 pm, "Gabriel Genellina" <>
    > wrote:
    > > Please provide more details. What do you want your program to do

    > while
    > > sleeping? What kind of actions do you want a response to?
    > > Do you have a GUI? A curses-based interfase?
    > >
    > > --
    > > Gabriel Genellina

    >
    > My app is purely console based. I just don't want the console to lock
    > up (on Windows using time.sleep(x) causes the console to become
    > unresponsive until the timer is done), and I want people to be able to
    > CTRL+C to stop the script if need be (which can't be done if it's
    > unresponsive!).
    >
    > Thanks.


    How about this? Responds to ctrl+c, but still sleeps.

    import time

    def responsiveSleep(n):
    while n > 0:
    time.sleep(1)
    n -= 1

    Cheers,

    Drea
    Andreas Tawn, Jan 29, 2010
    #4
  5. JohnnyFive

    MRAB Guest

    JohnnyFive wrote:
    > On Jan 28, 4:55 pm, "Gabriel Genellina" <>
    > wrote:
    >> Please provide more details. What do you want your program to do while
    >> sleeping? What kind of actions do you want a response to?
    >> Do you have a GUI? A curses-based interfase?
    >>
    >> --
    >> Gabriel Genellina

    >
    > My app is purely console based. I just don't want the console to lock
    > up (on Windows using time.sleep(x) causes the console to become
    > unresponsive until the timer is done), and I want people to be able to
    > CTRL+C to stop the script if need be (which can't be done if it's
    > unresponsive!).
    >
    > Thanks.


    Which version of Python are you using? time.sleep(x) can be interrupted
    in Python 2.6.

    If the version you're using can't be interrupted then you could use
    multiple sleeps:

    # Wait for a total of 10 secs.
    for i in range(10):
    time.sleep(1)
    MRAB, Jan 29, 2010
    #5
  6. En Fri, 29 Jan 2010 14:39:31 -0300, MRAB <>
    escribió:
    > JohnnyFive wrote:


    >> My app is purely console based. I just don't want the console to lock
    >> up (on Windows using time.sleep(x) causes the console to become
    >> unresponsive until the timer is done), and I want people to be able to
    >> CTRL+C to stop the script if need be (which can't be done if it's
    >> unresponsive!).
    >> Thanks.

    >
    > Which version of Python are you using? time.sleep(x) can be interrupted
    > in Python 2.6.


    I'm able to be more precise: time.sleep() can be interrupted in any Python
    version since 2.3.

    To the OP: beware of any unqualified 'except' clauses; a block like this:

    try:
    ...
    except:
    do_something_or_pass

    may "swallow" the KeyboardInterrupt exception (generated by a Ctrl-C
    press).
    From Python 2.5 and up, the most generic exception clause should read
    `except Exception: ...`
    In previous versions, you had to explicitely re-raise KeyboardInterrupt
    and SystemExit in any catch-all block:

    try:
    ...
    except (KeyboardInterrupt, SystemExit):
    raise
    except:
    ...

    --
    Gabriel Genellina
    Gabriel Genellina, Jan 29, 2010
    #6
  7. JohnnyFive

    JohnnyFive Guest

    On Jan 29, 9:33 am, Andreas Tawn <> wrote:
    > > On Jan 28, 4:55 pm, "Gabriel Genellina" <>
    > > wrote:
    > > > Please provide more details. What do you want your program to do

    > > while
    > > > sleeping? What kind of actions do you want a response to?
    > > > Do you have a GUI? A curses-based interfase?

    >
    > > > --
    > > > Gabriel Genellina

    >
    > > My app is purely console based. I just don't want the console to lock
    > > up (on Windows using time.sleep(x) causes the console to become
    > > unresponsive until the timer is done), and I want people to be able to
    > > CTRL+C to stop the script if need be (which can't be done if it's
    > > unresponsive!).

    >
    > > Thanks.

    >
    > How about this? Responds to ctrl+c, but still sleeps.
    >
    > import time
    >
    > def responsiveSleep(n):
    >     while n > 0:
    >         time.sleep(1)
    >         n -= 1
    >
    > Cheers,
    >
    > Drea


    Thanks for the ideas! Maybe it's just my computer, but using your
    solution still causes the prompt to become unresponsive during the
    sleeps.

    I am using 2.6.4 btw. It's not a major deal though, I just thought
    there had to be a way to do this fairly easily.
    JohnnyFive, Jan 29, 2010
    #7
  8. JohnnyFive

    Dave Angel Guest

    JohnnyFive wrote:
    > On Jan 29, 9:33 am, Andreas Tawn <> wrote:
    >
    >>> On Jan 28, 4:55 pm, "Gabriel Genellina" <>
    >>> wrote:
    >>>
    >>>> Please provide more details. What do you want your program to do
    >>>>
    >>> while
    >>>
    >>>> sleeping? What kind of actions do you want a response to?
    >>>> Do you have a GUI? A curses-based interfase?
    >>>>
    >>>> --
    >>>> Gabriel Genellina
    >>>>
    >>> My app is purely console based. I just don't want the console to lock
    >>> up (on Windows using time.sleep(x) causes the console to become
    >>> unresponsive until the timer is done), and I want people to be able to
    >>> CTRL+C to stop the script if need be (which can't be done if it's
    >>> unresponsive!).
    >>>
    >>> Thanks.
    >>>

    >> How about this? Responds to ctrl+c, but still sleeps.
    >>
    >> import time
    >>
    >> def responsiveSleep(n):
    >> while n > 0:
    >> time.sleep(1)
    >> n -=
    >>
    >> Cheers,
    >>
    >> Drea
    >>

    >
    > Thanks for the ideas! Maybe it's just my computer, but using your
    > solution still causes the prompt to become unresponsive during the
    > sleeps.
    >
    > I am using 2.6.4 btw. It's not a major deal though, I just thought
    > there had to be a way to do this fairly easily.
    >
    >

    You responded to my message off-list, so I have to paste it here.

    """<offlist>
    Dave,

    Thanks for the response.

    Here's my scenario. I have a program that checks an ftp site every hour for
    updated files, downloads them, and then sleeps again for another hour when
    done.

    It's 100% console.

    When I run the app, no matter how I try, I can't get the window to act
    normal during the sleep cycle. I've tried putting the sleep in another
    thread and have the main thread.join(), but it all has the same behavior:
    the console window behaves like it is crashing.

    Using the small sleep() increments also causes the same thing, the becomes
    undraggable for periods of time, and again acts like the program has crashed
    (even though it's just asleep).

    I am running a version of it right now, and i've managed to minimize it, but
    it won't maximize so I can ctrl+c it. All it's doing is exactly what Andreas
    recommended.

    This is not user-friendly behavior!

    I've also tried having the main thread sit at a raw_input, and have another
    thread have the timer, but there was odd behavior when I wanted the program
    to continue what it was doing, even if the user hadn't pressed "enter" to
    get passed the command prompt.

    </offlist> """


    You probably need to tell us your complete environment. I know you're
    running 2.6.4, but don't know which version of Windows, so I'll guess
    XP. You're running in a cmd.exe console.

    There must be something else going on in your system, since a console
    does not become unresponsive during a sleep. The python program is
    sleeping, but the console is very much alive; it's a separate process.
    So dragging, minimizing, restoring and maximizing is unaffected. Try
    the following simple script from a cmd console:

    import time
    print
    print
    print "going to sleep"
    print "Use Ctrl-C to get my attention, and end the program",
    time.sleep(30)
    print "done"


    On XP SP3, running Python 2.6.4, this ignores regular keystrokes, but
    responds nicely to control C. And it can be dragged around, resized,
    minimized, etc. with no problem.


    If you get different behavior, tell us more precisely how your
    environment differs from my guesses. Once we've solved Ctrl-C, drag the
    console, minimize the console, then maybe you're going to request
    "respond to other keystrokes". It can all be done, but only with more
    careful wording than "behaves like its crashing."

    Regards, DaveA
    Dave Angel, Jan 30, 2010
    #8
    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. Gonzalo Moreno
    Replies:
    2
    Views:
    12,373
  2. James Nugent
    Replies:
    4
    Views:
    776
  3. Sinan Nalkaya

    need help about time.sleep, timer

    Sinan Nalkaya, Nov 18, 2005, in forum: Python
    Replies:
    6
    Views:
    309
    Sinan Nalkaya
    Nov 22, 2005
  4. Sam Roberts
    Replies:
    16
    Views:
    362
    Daniel Berger
    Feb 18, 2005
  5. Richard
    Replies:
    7
    Views:
    213
    Richard
    May 22, 2007
Loading...

Share This Page