Python service gets interrupted function call

ashish ashish.jha at gmail.com
Wed Oct 10 06:11:04 EDT 2007


Hi All,
I wanted to know how to handle events like 'logoff' in the main thread
so that any process which is being run by svcDoRun method of service
does not get 'interrupted function call' exception.

I am posting a very simple service program , and i want to know that
is there a way to handle such interrupts without explicitly calling
try except block over blocking calls.

Here is the example which is getting interrupted exception at logoff.
=======================================================
import os, time, sys
import win32serviceutil, win32service
import pywintypes, win32con, winerror
# Use "import *" to keep this looking as much as a "normal" service
# as possible.  Real code shouldn't do this.
from win32event import *
from win32file import *
from win32pipe import *
from win32api import *
from ntsecuritycon import *

import servicemanager

import traceback
import thread, time

class TrialService(win32serviceutil.ServiceFramework):
    _svc_name_ = "TrialService"
    _svc_display_name_ = "TrialService"
    _svc_description_ = "TrialService"
    _exe_name_ = "C:/Python24/Lib/site-packages/win32/
pythonservice.exe"

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = CreateEvent(None, 0, 0, None)
        self.overlapped = pywintypes.OVERLAPPED()
        self.overlapped.hEvent = CreateEvent(None,0,0,None)

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

    def SvcDoRun(self):
        time.sleep(1000)

def ctrl_handler(ctrl_type):
    return True

if __name__ =='__main__':
    SetConsoleCtrlHandler(ctrl_handler, True)
    win32serviceutil.HandleCommandLine(TrialService)

========================================================

In actual call i just want to call my app in place of time.sleep but
my app will have blocking code segments.
Any help will be greatly appreciated .
Thanks
Ashish




More information about the Python-list mailing list