How to build a Windows service using win32?

2007-03-07 Thread Gregor Mosheh
I'm trying to write a Win32 service. The following is straight from Python
Programming on Win32 and it doesn't work. Is that book out of date; is
there a new way to do services? I searched Google for hours trying to find
any other method, and have been beating on this one for 5 more hours.

The present error is:

C:\Tester>python tester.py debug
Debugging service Tester - press Ctrl+C to stop.
Error 0xC004 - Python could not import the service's module

: No module named service


The code is:

import win32serviceutil, win32service, win32event

class Service(win32serviceutil.ServiceFramework):
_svc_name_ = "EDMS-to-CG"
_svc_display_name_ = "EDMS-to-CG Syncer"
_svc_description_  = "Uploaded the EDMS database to Cartograph"

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
pausetime = 60 * 1000
while True:
stopsignal = win32event.WaitForSingleObject(self.hWaitStop,
pausetime)
if stopsignal == win32event.WAIT_OBJECT_0: break
self.runOneLoop()

def runOneLoop(self):
import servicemanager
servicemanager.LogInfoMsg('Running')

win32serviceutil.HandleCommandLine(Service)


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to build a Windows service using win32?

2007-03-07 Thread Gregor Mosheh

Giles Brown wrote:
> Yeah.  You've cleverly decided to simplify the "smallest possible
> python service" by removing the
> if __name__ == '__main__':

Ha ha. :)
Seriously, though, I removed that long after it was failing to work, and
have since replaced it and it didn't affect a thing.


Gabriel Genellina wrote:
> Have you installed the pywin32 package from
> https://sourceforge.net/projects/pywin32/ ?
> The code skeleton looks OK to me.

Indeed, I have win32 installed. I'm used to writing wx applications,
compiling into exes, etc. Services are a new direction for me.


So, both of you say that the program I sent *did* work?import win32serviceutil, win32service, win32event


class Service(win32serviceutil.ServiceFramework):
_svc_name_ = "EDMS-to-CG"
_svc_display_name_ = "EDMS-to-CG Syncer"
_svc_description_  = "Uploaded the EDMS database to Cartograph"

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
pausetime = 60 * 1000
while True:
stopsignal = win32event.WaitForSingleObject(self.hWaitStop, 
pausetime)
if stopsignal == win32event.WAIT_OBJECT_0: break
self.runOneLoop()

def runOneLoop(self):
import servicemanager
servicemanager.LogInfoMsg('Running')

if __name__ == '__main__':
win32serviceutil.HandleCommandLine(Service)-- 
http://mail.python.org/mailman/listinfo/python-list

Re: How to build a Windows service using win32?

2007-03-07 Thread Gregor Mosheh
Giles Brown wrote:
> Yeah.  You've cleverly decided to simplify the "smallest
> possible python service" by removing the
> if __name__ == '__main__':

Ha ha. :)
Seriously, though, I removed that long after it was failing to work, and
have since replaced it and it didn't affect a thing.


Gabriel Genellina wrote:
> Have you installed the pywin32 package from
> https://sourceforge.net/projects/pywin32/ ?
> The code skeleton looks OK to me.

Indeed, I have win32 installed. :)

I'm used to writing wxPython apps, even a small-scale video game, but
services are a new and alien direction for me. :)

Now, I did stumble upon the solution to this one this morning, after a
fresh night of sleep: I re-ran "python tester.py install" I got a message
that the service had been updated, and now it runs! Hooray!

Any insight into why that would be the case? Does pythonservice.exe keep a
cached copy or some such, so I'll have to re-install the service after
code changes?


-- 
http://mail.python.org/mailman/listinfo/python-list