win32serviceutil.ServiceFramework
class in his book Python Programming on Win32.The problem is, that if you use a breakpoint in your service code and try to debug it, you'll find that it never stops at your breakpoint. The reason is that the
win32serviceutil
module will actually run PythonService.exe and have that program run your python script. Since PythonService.exe is its own process that has no knowledge of your IDE's breakpoints, it just runs without breaking.Here is a simple trick for the small intersection of people who:
- Write Windows Services
- in Python
- using an IDE (I use pyDev)
- and need to step through their code to debug.
It turns out that
win32serviceutil
has a reimplementation of the logic in PythonService.exe that it uses to emulate PythonService.exe's debug mode when you convert your python script you an executable (e.g. via py2exe). The implementation is in win32serviceutil
's DebugService()
function. The trick to being able to set breakpoints and debug your python service is to convince win32serviceutil
into calling DebugService
rather than spawning PythonService.exe.Luckily, this is trivially easy: just add the line
sys.frozen = 'windows_exe' # Fake py2exe so we can debug
before you call
win32serviceutil.HandleCommandLine(...)
.Then, you just need to pass the '-debug' command-line argument when you run your service to force it into debugging mode. Your debugger should then control the process so you can debug it.
3 comments:
Um, doesn't import pdb; pdb.set_trace() work?
No, it does not, as mentioned in the original post. PythonService.exe do not provide a prompt for you to interact with the debugger.
Good information provided by thanks keep updating
python online training
Post a Comment