Python script running as Windows service: Clean shutdown

Discussion in 'Python' started by Stefan Krah, Jan 11, 2006.

  1. Stefan Krah

    Stefan Krah Guest

    Hello,

    I'm trying to run a Python script as a Windows service with a defined
    shutdown. The script (enigma-client.py) handles the communications with
    the server in a distributed computing effort and calls a C program
    (enigma.exe) to do the computations.

    enigma.exe should save its current state when receiving SIGINT or SIGTERM.
    This (obviously) works under Unix and also when running the script from the
    Windows command line and terminating it with Ctrl-C.

    I understand that for a clean shutdown of a Windows service one would
    have to use the win32 extensions and have the working thread check
    for the shutdown event in short intervals (<ddtp0q$jga$>).


    This would leave me with these options:

    a) Write enigma-client.py as a service. Write enigma.exe as a
    service and have it poll regularly for shutdown events.

    b) Have enigma.exe save its state regularly, use srvany.exe
    and forget about a graceful shutdown.


    I'm new to Windows services, so I'd be grateful for corrections or
    better solutions. Here is relevant part of the code (The whole thing
    is at http://www.bytereef.org/enigma-client.txt ):



    """ main """
    cmdline = 'enigma.exe -R 00trigr.naval 00bigr.naval 00ciphertext > NUL'
    eclient = Eclient()

    if len(sys.argv) != 3:
    eclient.usage()

    if os.path.isfile(LOCKFILE):
    print "enigma-client: error: found lockfile %s. \n" \
    "Check that no other enigma-client process is using this directory." \
    % LOCKFILE
    sys.exit(1)

    atexit.register(eclient.rm_lockfile)
    eclient.touch_lockfile()

    win32process.SetPriorityClass(
    win32process.GetCurrentProcess(),
    win32process.IDLE_PRIORITY_CLASS
    )

    while 1:
    retval = os.system(cmdline) >> 8
    if retval == 0:
    eclient.submit_chunk(sys.argv[1], int(sys.argv[2]))
    eclient.get_chunk(sys.argv[1], int(sys.argv[2]))
    elif retval == 1:
    eclient.get_chunk(sys.argv[1], int(sys.argv[2]))
    time.sleep(10)
    else:
    """./enigma has caught a signal"""
    sys.exit(retval)



    Stefan Krah

    --
    Break original Enigma messages: http://www.bytereef.org/m4_project.html
     
    Stefan Krah, Jan 11, 2006
    #1
    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. N. Hamacher

    Recognize Windows Shutdown

    N. Hamacher, Jun 21, 2004, in forum: Java
    Replies:
    2
    Views:
    3,515
    Mark Thornton
    Jun 21, 2004
  2. hyperbob

    shutdown in windows

    hyperbob, Sep 10, 2004, in forum: Python
    Replies:
    0
    Views:
    283
    hyperbob
    Sep 10, 2004
  3. Aspersieman
    Replies:
    1
    Views:
    391
    Mike Driscoll
    May 13, 2008
  4. Replies:
    8
    Views:
    533
  5. Eric Peterson

    shutdown of linux box from cron perl script

    Eric Peterson, Jan 6, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    466
    Steven_Smith
    Jan 10, 2005
Loading...

Share This Page