[python-win32] Windows service problem

Larry Bates larry.bates at websafe.com
Tue Jun 12 16:24:03 CEST 2007


Dalton, Tom wrote:
> Hi,
> 
> I'm using pywin32 on WinXP embedded to run a server service (using
> Win32serviceutil.py). The service is installed correctly and set up to
> run automatically on startup, which it does. The machine boots to the
> logon screen and the service can be verified to be running correctly.
> 
> If I now log onto the machine as administrator, the service is still
> running. When I log off however, I return to the windows logon screen,
> but the service seems to have stopped. Logging back in as administrator,
> the Windows services screen says the service has stopped. When I check
> the system log for applications, the following is logged:
> 
> The instance's SvcRun() method failed
>   File "C:\python24\lib\site-packages\win32\lib\win32serviceutil.py"
> line 742, in self.SvcDoRun()
>   File "C:\myservice\myservice.py" line 123 in SvcDoRun time.sleep(5)
> exceptions.IOError: (4, "Interrupted function call")
> 
> What does this mean? Why does logging out of the Admin account affect a
> background service? Is this a known issue? I've copied the example and
> modified very little. My service is effectively an infinite loop that
> periodically checks if the 'real' server process (another python
> program) has died, and if so restarts it. Is this some weird issue with
> using sleep within the service's main method? If so, how can I get round
> this problem?
> 
> I'm using pywin 2.0.4 and python 2.4.3
> 
> Thanks for any help anyone can give :-)
> 
> Tom
> 
>My service is effectively an infinite loop that
> periodically checks if the 'real' server process (another python
> program) has died

All services are infinite loops that only respond to stop signals.

I don't know about the specifics of your question but why would you
want to put time.sleep() in a service?  Services have their own
method timeout value in:

win32event.WaitForSIngleObject(self.hWaitStop, self.timeout)

call for "sleeping" time.sleep() takes CPU time that is unnecessary
and is most likely your problem.  What you have discovered may in
fact be a bug, but it isn't the correct way to sleep in a service
either.  Change your monitoring service so that it uses the timeout
value as a sleep interval and it will work.

I might also suggest an alternative method.  I had a service that
would run for weeks an then die unexpectedly.  I've tried for some
time to track down the problem to no avail.  To work around the
problem I wrote a standalone program that I run every 30 minutes
using the Windows scheduler.  It checks to see if the service is
running and if it isn't it restarts it.  I'm going to send you a
copy via email.  You won't be able to use it as it is, but maybe
it will help.

-Larry




More information about the Python-win32 mailing list