win32 service and time.sleep()

Discussion in 'Python' started by rbt, Sep 20, 2005.

  1. rbt

    rbt Guest

    I have a win32 service written in Python. It works well. It sends a
    report of the status of the machine via email periodically. The one
    problem I have is this... while trying to send an email, the script
    loops until a send happens and then it breaks. Should it be unable to
    send, it sleeps for 10 minutes with time.sleep(600) and then wakes and
    tries again. This is when the problem occurs. I can't stop the service
    while the program is sleeping. When I try, it just hangs until a reboot.
    Can some suggest how to fix this?

    Thanks,
    rbt
     
    rbt, Sep 20, 2005
    #1
    1. Advertising

  2. rbt

    Oracle Guest

    On Tue, 20 Sep 2005 10:49:13 -0400, rbt wrote:

    > I have a win32 service written in Python. It works well. It sends a
    > report of the status of the machine via email periodically. The one
    > problem I have is this... while trying to send an email, the script
    > loops until a send happens and then it breaks. Should it be unable to
    > send, it sleeps for 10 minutes with time.sleep(600) and then wakes and
    > tries again. This is when the problem occurs. I can't stop the service
    > while the program is sleeping. When I try, it just hangs until a reboot.
    > Can some suggest how to fix this?
    >


    You could try doing it the hard way. In a loop, sleep for 1-5 seconds at
    a time. Everytime you complete a sleep, check the elapsed time. If the
    elapsed time is >= the total sleep duration, fall out of the sleep loop
    and try your email again. This should allow your service to stop within
    1-5 seconds of your request while imposing little to no load on the system.
     
    Oracle, Sep 20, 2005
    #2
    1. Advertising

  3. Oracle wrote:
    > On Tue, 20 Sep 2005 10:49:13 -0400, rbt wrote:
    >
    >> I have a win32 service written in Python. It works well. It sends a
    >> report of the status of the machine via email periodically. The one
    >> problem I have is this... while trying to send an email, the script
    >> loops until a send happens and then it breaks. Should it be unable to
    >> send, it sleeps for 10 minutes with time.sleep(600) and then wakes and
    >> tries again. This is when the problem occurs. I can't stop the service
    >> while the program is sleeping. When I try, it just hangs until a reboot.
    >> Can some suggest how to fix this?
    >>

    >
    > You could try doing it the hard way. In a loop, sleep for 1-5 seconds at
    > a time. Everytime you complete a sleep, check the elapsed time. If the
    > elapsed time is >= the total sleep duration, fall out of the sleep loop
    > and try your email again. This should allow your service to stop within
    > 1-5 seconds of your request while imposing little to no load on the system.


    I adopted that solution just last week. When you wake (every few
    seconds), you check for a stop flag (and exit if needed of
    course) before checking if its time to do work-work. Works a treat.

    Another option is to use a threading.Condition, and have the
    service thread wait(600). The thread that calls to stop the
    service can set the stop flag and then notify() the Condition.

    Steve
     
    Steve Horsley, Sep 22, 2005
    #3
    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. Rick
    Replies:
    2
    Views:
    2,517
    Roedy Green
    Oct 4, 2003
  2. Gonzalo Moreno
    Replies:
    2
    Views:
    12,571
  3. Angus Comber
    Replies:
    6
    Views:
    1,179
    Keith Thompson
    Oct 12, 2005
  4. Sam Roberts
    Replies:
    16
    Views:
    410
    Daniel Berger
    Feb 18, 2005
  5. Richard
    Replies:
    7
    Views:
    261
    Richard
    May 22, 2007
Loading...

Share This Page