a bug in python windows service?

M

momobear

I feel really puzzled about fellowing code, please help me finger out
what problem here.

import threading

class workingthread(threading.Thread):
def __init__(self):
self.quitEvent = threading.Event()
self.waitTime = 10
threading.Thread.__init__(self)

def run(self):
while not self.quitEvent.isSet():
self.quitEvent.wait(self.waitTime)

def join(self, timeout = None):
self.quitEvent.set()
threading.Thread.join(self, timeout)

import win32serviceutil
import win32event

class testTime(win32serviceutil.ServiceFramework):
_svc_name_ = "testTime"
_svc_display_name_ = "testTime"
_svc_deps_ = ["EventLog"]

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

def SvcStop(self):
win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
self.thread.run()
win32event.WaitForSingleObject(self.hWaitStop,
win32event.INFINITE)
self.thread.join()

if __name__ == '__main__':
win32serviceutil.HandleCommandLine(testTime)

each time I got the fellowing result, anyone can point out what's
wrong in it?

E:\code\monitor2>testTime.py debug
Debugging service testTime- press Ctrl+C to stop.
Stopping debug service.
Error 0xC0000003 - The instance's SvcRun() method failed

File "C:\Python24\Lib\site-packages\win32\lib\win32serviceutil.py",
line 785,
in SvcRun
self.SvcDoRun()
File "E:\code\monitor2\testTime.py", line 35, in SvcDoRun
self.thread.run()
File "E:\code\monitor2\testTime.py", line 12, in run
self.quitEvent.wait(self.waitTime)
File "C:\Python24\lib\threading.py", line 348, in wait
self.__cond.wait(timeout)
File "C:\Python24\lib\threading.py", line 222, in wait
_sleep(delay)

exceptions.IOError: (4, 'Interrupted function call')
 
G

Gabriel Genellina

I feel really puzzled about fellowing code, please help me finger out
what problem here.

import threading

class workingthread(threading.Thread):
def __init__(self):
self.quitEvent = threading.Event()
self.waitTime = 10
threading.Thread.__init__(self)

def run(self):
while not self.quitEvent.isSet():
self.quitEvent.wait(self.waitTime)

def join(self, timeout = None):
self.quitEvent.set()
threading.Thread.join(self, timeout)

import win32serviceutil
import win32event

class testTime(win32serviceutil.ServiceFramework):
_svc_name_ = "testTime"
_svc_display_name_ = "testTime"
_svc_deps_ = ["EventLog"]

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

def SvcStop(self):
win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
self.thread.run()
win32event.WaitForSingleObject(self.hWaitStop,
win32event.INFINITE)
self.thread.join()

No, this is not a bug. You must not call Thread.run(), use Thread.start()
instead - else your code won't run in a different thread of execution. See
http://docs.python.org/lib/thread-objects.html on how to use Thread
objects - and note that you should *only* override __init__ and run, if
any.
Instead of extending join(), write a specific method to signal the
quitEvent or just let the caller signal it. And I don't see in this
example why do you need two different events (one on the thread, another
on the service controller), a single event would suffice.
 
M

momobear

No, this is not a bug. You must not call Thread.run(), use Thread.start()
instead - else your code won't run in a different thread of execution. See http://docs.python.org/lib/thread-objects.htmlon how to use Thread
objects - and note that you should *only* override __init__ and run, if
any.
Instead of extending join(), write a specific method to signal the
quitEvent or just let the caller signal it. And I don't see in this
example why do you need two different events (one on the thread, another
on the service controller), a single event would suffice.

Thanks for help, It works now:D
 
M

momobear

Instead of extending join(), write a specific method to signal the
quitEvent or just let the caller signal it. And I don't see in this
example why do you need two different events (one on the thread, another
on the service controller), a single event would suffice.

I don't think a single event is enought, since I think the event
python created and windows event are not same kind of event.
 
G

Gabriel Genellina

I don't think a single event is enought, since I think the event
python created and windows event are not same kind of event.

They are not the same object, of course (altough the threading.Event
object relies eventually on a mutex implemented using CreateEvent). But in
this case both can be successfully used; of course, having the Python
object a more "pythonic" interfase (not a surprise!), it's easier to use.
The same example modified using only a threading.Event object (and a few
messages to verify how it runs):

import threading
from win32api import OutputDebugString as ODS

class workingthread(threading.Thread):
def __init__(self, quitEvent):
self.quitEvent = quitEvent
self.waitTime = 1
threading.Thread.__init__(self)

def run(self):
while not self.quitEvent.isSet():
ODS("Running...\n")
self.quitEvent.wait(self.waitTime)
ODS("Exit run.\n")


import win32serviceutil
import win32event

class testTime(win32serviceutil.ServiceFramework):
_svc_name_ = "testTime"
_svc_display_name_ = "testTime"
_svc_deps_ = ["EventLog"]

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = threading.Event()
self.thread = workingthread(self.hWaitStop)

def SvcStop(self):
self.hWaitStop.set()

def SvcDoRun(self):
self.thread.start()
self.hWaitStop.wait()
self.thread.join()

if __name__ == '__main__':
win32serviceutil.HandleCommandLine(testTime)
 
M

momobear

I don't think a single event is enought, since I think the event
python created and windows event are not same kind of event.

They are not the same object, of course (altough the threading.Event
object relies eventually on a mutex implemented using CreateEvent). But in
this case both can be successfully used; of course, having the Python
object a more "pythonic" interfase (not a surprise!), it's easier to use.
The same example modified using only a threading.Event object (and a few
messages to verify how it runs):

import threading
from win32api import OutputDebugString as ODS

class workingthread(threading.Thread):
def __init__(self, quitEvent):
self.quitEvent = quitEvent
self.waitTime = 1
threading.Thread.__init__(self)

def run(self):
while not self.quitEvent.isSet():
ODS("Running...\n")
self.quitEvent.wait(self.waitTime)
ODS("Exit run.\n")

import win32serviceutil
import win32event

class testTime(win32serviceutil.ServiceFramework):
_svc_name_ = "testTime"
_svc_display_name_ = "testTime"
_svc_deps_ = ["EventLog"]

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = threading.Event()
self.thread = workingthread(self.hWaitStop)

def SvcStop(self):
self.hWaitStop.set()

def SvcDoRun(self):
self.thread.start()
self.hWaitStop.wait()
self.thread.join()

if __name__ == '__main__':
win32serviceutil.HandleCommandLine(testTime)

Great! thanks, now I understand the real work of the python windows
service.
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top