stopping a python windows service

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DK

    #1

    stopping a python windows service

    i was able to successfully create a windows service using py2exe. it
    polls a website periodically and logs it to a file. this is done using
    a function that does an infinite loop with periodic "sleeps".

    my question is...

    what's the best way to stop this service gracefully?

    when try to stop it from the services applet from control panel, it
    takes forever and then gives me an error.

    currently, the only way i am able to stop it is using the task manager
    and killing the process.

  • Benjamin Niemann

    #2
    Re: stopping a python windows service

    DK wrote:
    [color=blue]
    > i was able to successfully create a windows service using py2exe. it
    > polls a website periodically and logs it to a file. this is done using
    > a function that does an infinite loop with periodic "sleeps".
    >
    > my question is...
    >
    > what's the best way to stop this service gracefully?
    >
    > when try to stop it from the services applet from control panel, it
    > takes forever and then gives me an error.
    >
    > currently, the only way i am able to stop it is using the task manager
    > and killing the process.[/color]

    Windows services generally use two threads: one to do the work and one to
    listen for messages from the
    whatever-the-component-is-called-to-control-services.
    When the message thread received a 'stop' message, it should inform the
    worker thread to shut down, e.g. using threading.Event . So your worker
    should regularily check for the shutdown event, e.g.:

    while not shutdownEvent.i sset():
    pollWebsite()

    for i in xrange(1800):
    if shutdownEvent.i sset():
    break
    time.sleep(1)

    But if you get the 'stop' message while the worker thread is in
    pollWebsite() and the webserver is sloooow, you'll still have a significant
    delay... To avoid this, you would need a http client based on select() that
    allows you to check shutdownEvent.i sset() at certain intervals - instead of
    urlopen which just blocks.


    --
    Benjamin Niemann
    Email: pink at odahoda dot de
    WWW: http://www.odahoda.de/

    Comment

    • Grig Gheorghiu

      #3
      Re: stopping a python windows service

      Here are 2 recipes from the online Python Cookbook. I've used this one
      very successfully:
      <http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/115875>.

      This one seems simpler:
      <http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/59872>

      Grig

      Comment

      • Do Re Mi chel La Si Do

        #4
        Re: stopping a python windows service

        Hi !

        Use SC.exe (windows-XP) (with popen ?)
        For help : sc /?


        You can, also, try :
        qprocess /?
        tasklist /?
        taskkill /?
        etc.

        @-salutations

        Michel Claveau



        Comment

        • DK

          #5
          Re: stopping a python windows service

          I may have taken your code example too literally. I tried putting in
          the check for 'shutdownEvent. isset()' but it's failing at run time.
          It's looking for a global variable, I guess.

          Do I have to register these threads somehow in the beginning?

          I'm somewhat new to Python so please be patient...

          Comment

          • Dennis Lee Bieber

            #6
            Re: stopping a python windows service

            On 17 Aug 2005 06:20:46 -0700, "DK" <provato@gmail. com> declaimed the
            following in comp.lang.pytho n:
            [color=blue]
            > I may have taken your code example too literally. I tried putting in
            > the check for 'shutdownEvent. isset()' but it's failing at run time.
            > It's looking for a global variable, I guess.
            >[/color]
            "shutdownEv ent" is an event instance created using the threading
            module.
            [color=blue]
            > Do I have to register these threads somehow in the beginning?
            >[/color]
            You'll have to create an Event object, a worker thread, and perhaps
            leave the main thread as the monitor for system shutdown events.
            [color=blue]
            > I'm somewhat new to Python so please be patient...[/color]

            Why do I find it discomforting that people new to the language keep
            wanting to do deep OS specific actions in their first week?

            {Granted, my first program was a rudimentary "sendmail" for my Amiga,
            using ARexx scripts from the email client to queue messages into a temp
            directory, and waking up, if needed, the Python program to parse the
            to:, cc:, and bcc: headers, handshaking with the ISP's outgoing daemon
            -- but everything I wrote was native Python; no OS specific code.
            I needed to do this as the downloadable "sendmails" had major flaws:
            the first one created a message file for each recipient, and would get
            stuck if one of the recipient addresses wasn't receiving (this was back
            in the days before ISPs locked port 25 passthrough); the second relayed
            via the ISP -- but did not handshake the cc/bcc addresses, so such never
            received the mail.}
            --[color=blue]
            > =============== =============== =============== =============== == <
            > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
            > wulfraed@dm.net | Bestiaria Support Staff <
            > =============== =============== =============== =============== == <
            > Home Page: <http://www.dm.net/~wulfraed/> <
            > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

            Comment

            • Peter Hansen

              #7
              Re: stopping a python windows service

              DK wrote:[color=blue]
              > I may have taken your code example too literally. I tried putting in
              > the check for 'shutdownEvent. isset()' but it's failing at run time.
              > It's looking for a global variable, I guess.[/color]

              Or perhaps "it" is just looking for correct capitalization, since Python
              is case sensitive. Try shutdownEvent.i sSet() instead.

              -Peter

              Comment

              Working...