Python service gets interrupted function call

A

ashish

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
 
R

Roger Upole

ashish said:
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

Use win32api.Sleep, and pass True for the second arg.

Roger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top