On 12/05/2010 4:28 AM, Bill Janssen wrote:
OK, so I wrote my own InstallService:

That actually isn't necessary - the class can provide _exe_name_ and _exe_args_ attributes to override the defaults.

See attached sample which works for me - be sure to place it in the same directory as the pipeTestService.py sample (win32/Demos/service). I'm afraid I did mislead you a little in the steps necessary to get things going, but I'm checking this sample into pywin32 so it doesn't get lost again :)

Cheers,

Mark
# This is an example of a service hosted by python.exe rather than 
# pythonservice.exe.

# Note that it is very rare that using python.exe is a better option
# than the default pythonservice.exe - the latter has better error handling
# so that if Python itself can't be initialized or there are very early
# import errors, you will get error details written to the event log.  When
# using python.exe instead, you are forced to wait for the interpreter startup
# and imports to succeed before you are able to effectively setup your own
# error handling.

# So in short, please make sure you *really* want to do this, otherwise just
# stick with the default.

import sys
import os
import win32serviceutil
import servicemanager

from pipeTestService import TestPipeService

class NativeTestPipeService(TestPipeService):
    _svc_name_ = "PyNativePipeTestService"
    _svc_display_name_ = "Python Native Pipe Test Service"
    _svc_description_ = "Tests Python.exe hosted services"
    # tell win32serviceutil we have a custom executable and custom args
    # so registration does the right thing.
    _exe_name_ = sys.executable
    _exe_args_ = '"' + os.path.abspath(sys.argv[0]) + '"'

def main():
    if len(sys.argv)==1:
        # service must be starting...
        # for the sake of debugging etc, we use win32traceutil to see
        # any unhandled exceptions and print statements.
        import win32traceutil
        print "service is starting..."
        print "(execute this script with '--help' if that isn't what you want)"
        
        servicemanager.Initialize()
        servicemanager.PrepareToHostSingle(NativeTestPipeService)
        # Now ask the service manager to fire things up for us...
        servicemanager.StartServiceCtrlDispatcher()
        print "service done!"
    else:
        win32serviceutil.HandleCommandLine(NativeTestPipeService)

if __name__=='__main__':
    try:
        main()
    except (SystemExit, KeyboardInterrupt):
        raise
    except:
        print "Something went bad!"
        import traceback
        traceback.print_exc()
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to