From: David B. <db3...@gm...> - 2007-11-12 01:50:09
|
Jason Hiltz-Laforge <ja...@jy...> writes: > works like a charm. In the Windows service list, it now points to > MyService.exe. However, when I try to do this: > > MyService.exe start > > Windows complains that the service did not respond in a timely manner > and sure enough, it doesn't start. What am I doing wrong here? Here's > the code for the service. I'm using py2exe 0.6.6 and python 2.5. I think you're running into the fact that different startup code runs depending on whether you run the script directly or as a built executable. The "start" command line option is only supported in the former case. So instead of starting the registered service, your local execution tries to start up as a service, which won't work since it hasn't actually been started by the service manager. Using a "start" argument to the service executable is not a typical method for starting a service. Normally controlling a service from the command line is done via the "net" command. For example, "net start <servicename>" to start a service. When running as a script, the win32serviceutil.HandleCommandLine() method handles command line parsing and support for service installation/removal. It happens to have start/stop/restart support, but in turn it just executes the equivalent API to start the service as a separate process, unrelated to the executable you are running. So there's no benefit to doing it this way rather than using the "net" command. When running as a built executable, the startup code from the boot_service module within py2exe has control. While it has similar startup behavior and also supports service installation/removal, it's not identical in behavior to the win32serviceutil implementation. If I recall, in addition, neither of those implementations work very well if you want to run the service directly (for stdout/stderr debugging) without involving startup through the service manager. If all you want is a consistent way to control execution of the service, I'd just switch to the "net" command, as it requires nothing special from the service once registered, and works for any service, not just Python ones. If you have other startup requirements, for cases where I wanted consistent behavior (with debug control), I've used a custom version of boot_service, which I then ran for normal startup as well. You could do something similar and add start/stop support to it if you wanted. For example, given some local implementation of boot_service.py, you can use it for normal script execution as: if __name__ == "__main__": # Use the same boot code that we get when packaged import boot_service boot_service.main() and then make sure that your local boot_service module is used for the built version via the following in your setup.py: from py2exe.build_exe import py2exe class Py2exe(py2exe): # Add any other local customizations def get_boot_script(self, boot_type): # Override boot script when building a service if boot_type != 'service': return py2exe.get_boot_script(self, boot_type) else: return os.path.join(os.path.dirname(__file__), "boot_" + boot_type + ".py") setup( # ... any normal options ... cmdclass = {'py2exe': Py2exe}) -- David |