473,394 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

a bug in python windows service?

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')

May 27 '07 #1
5 3911
En Sat, 26 May 2007 23:00:45 -0300, momobear <wg****@gmail.comescribió:
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.

--
Gabriel Genellina

May 27 '07 #2
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.

--
Gabriel Genellina
Thanks for help, It works now:D
May 27 '07 #3
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.
May 27 '07 #4
En Sun, 27 May 2007 09:07:36 -0300, momobear <wg****@gmail.comescribió:
>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.
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)

--
Gabriel Genellina

May 27 '07 #5
On May 27, 11:25 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Sun, 27 May 2007 09:07:36 -0300, momobear <wgw...@gmail.comescribió:
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.

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)

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

May 28 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gandalf | last post by:
Can anybody tell me how to write a windows service in Python? I would only use file operations (maybe the logger module) and TCP/IP. I already have my server and it runs fine both on Windows, Linux...
0
by: Nazgul | last post by:
Hi! Sorry if I posted it twice... I need your help... I have the following problem. I've implemented the python Windows Service which behaves like a log supervisor. If the space used by log...
0
by: David Mitchell | last post by:
Hello group, I'm trying to create a TCP server using Python, and I want it to run under Windows as a service. Now, I'm fine with building the TCP server using Python - done it lots of times,...
3
by: David Fraser | last post by:
Hi We are trying to debug a problem with services created using py2exe. It seems that these problems have arisen after services were installed and removed a few times. OK, first the actual...
8
by: Saravanan | last post by:
Hello, Im running Python Application as a Windows Service (using windows extensions). But, sporadically the application crashes (crash in Python23.dll) and this stops the service. This problem...
0
by: Saravanan | last post by:
Hello, Im using Python 2.3.3 along with Win32all (163). Currently my python application runs as Windows Service. Im using Win32all Service Framework to run the Python Code as a Windows Service....
8
by: Jan Gregor | last post by:
Hello I run python script on another computer and want to "survive" that script after my logout. the script also uses drive mapping to network drive. Can you help me ? Or better is there some...
3
by: zxo102 | last post by:
Hi there, I have a python application (many python scripts) and I start the application like this python myServer.py start in window. It is running in dos window. Now I would like to put it...
0
by: Stefan Krah | last post by:
Hello, I'm trying to run a Python script as a Windows service with a defined shutdown. The script (enigma-client.py) handles the communications with the server in a distributed computing effort...
1
by: Aspersieman | last post by:
Hi All I have a windows service (attached file). I basically just calls another script every 60 seconds. I can install, start and stop this service as expected with: ParseMailboxService.py...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.