473,748 Members | 10,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(t hreading.Thread ):
def __init__(self):
self.quitEvent = threading.Event ()
self.waitTime = 10
threading.Threa d.__init__(self )

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

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

import win32serviceuti l
import win32event

class testTime(win32s erviceutil.Serv iceFramework):
_svc_name_ = "testTime"
_svc_display_na me_ = "testTime"
_svc_deps_ = ["EventLog"]

def __init__(self, args):
win32serviceuti l.ServiceFramew ork.__init__(se lf, args)
self.hWaitStop = win32event.Crea teEvent(None, 0, 0, None)
self.thread = workingthread()

def SvcStop(self):
win32event.SetE vent(self.hWait Stop)

def SvcDoRun(self):
self.thread.run ()
win32event.Wait ForSingleObject (self.hWaitStop ,
win32event.INFI NITE)
self.thread.joi n()

if __name__ == '__main__':
win32serviceuti l.HandleCommand Line(testTime)

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

E:\code\monitor 2>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\Li b\site-packages\win32\ lib\win32servic eutil.py",
line 785,
in SvcRun
self.SvcDoRun()
File "E:\code\monito r2\testTime.py" , line 35, in SvcDoRun
self.thread.run ()
File "E:\code\monito r2\testTime.py" , line 12, in run
self.quitEvent. wait(self.waitT ime)
File "C:\Python24\li b\threading.py" , line 348, in wait
self.__cond.wai t(timeout)
File "C:\Python24\li b\threading.py" , line 222, in wait
_sleep(delay)

exceptions.IOEr ror: (4, 'Interrupted function call')

May 27 '07 #1
5 3964
En Sat, 26 May 2007 23:00:45 -0300, momobear <wg****@gmail.c omescribió:
I feel really puzzled about fellowing code, please help me finger out
what problem here.

import threading

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

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

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

import win32serviceuti l
import win32event

class testTime(win32s erviceutil.Serv iceFramework):
_svc_name_ = "testTime"
_svc_display_na me_ = "testTime"
_svc_deps_ = ["EventLog"]

def __init__(self, args):
win32serviceuti l.ServiceFramew ork.__init__(se lf, args)
self.hWaitStop = win32event.Crea teEvent(None, 0, 0, None)
self.thread = workingthread()

def SvcStop(self):
win32event.SetE vent(self.hWait Stop)

def SvcDoRun(self):
self.thread.run ()
win32event.Wait ForSingleObject (self.hWaitStop ,
win32event.INFI NITE)
self.thread.joi n()
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.c omescribió:
>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 OutputDebugStri ng as ODS

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

def run(self):
while not self.quitEvent. isSet():
ODS("Running... \n")
self.quitEvent. wait(self.waitT ime)
ODS("Exit run.\n")
import win32serviceuti l
import win32event

class testTime(win32s erviceutil.Serv iceFramework):
_svc_name_ = "testTime"
_svc_display_na me_ = "testTime"
_svc_deps_ = ["EventLog"]

def __init__(self, args):
win32serviceuti l.ServiceFramew ork.__init__(se lf, args)
self.hWaitStop = threading.Event ()
self.thread = workingthread(s elf.hWaitStop)

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

def SvcDoRun(self):
self.thread.sta rt()
self.hWaitStop. wait()
self.thread.joi n()

if __name__ == '__main__':
win32serviceuti l.HandleCommand Line(testTime)

--
Gabriel Genellina

May 27 '07 #5
On May 27, 11:25 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Sun, 27 May 2007 09:07:36 -0300, momobear <wgw...@gmail.c omescribió:
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 OutputDebugStri ng as ODS

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

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

import win32serviceuti l
import win32event

class testTime(win32s erviceutil.Serv iceFramework):
_svc_name_ = "testTime"
_svc_display_na me_ = "testTime"
_svc_deps_ = ["EventLog"]

def __init__(self, args):
win32serviceuti l.ServiceFramew ork.__init__(se lf, args)
self.hWaitStop = threading.Event ()
self.thread = workingthread(s elf.hWaitStop)

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

def SvcDoRun(self):
self.thread.sta rt()
self.hWaitStop. wait()
self.thread.joi n()

if __name__ == '__main__':
win32serviceuti l.HandleCommand Line(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
1311
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 and FreeBSD maybe others too, who knows? :-) The only problem is that it should be a service. It is easy to make it a service on a UNIX machine but I need to run as a Windows service on NT/2000/XP. Is this possible? Can you show me a link to an...
0
1549
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 files is bigger than a given upper limit, then it starts to delete log files until the space is less than a given lower limit. I configured the service to start up automatically on system boot. The script checks the space used by log files every 1000...
0
1491
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, and I know there's lots of sample code out there I can grab if I ever need to. Equally, I think I've got the simpler concepts about creating Windows
3
5216
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 problem we're seeing. After compiling a service with py2exe, running "service -install" and attempting to start it from the Services dialog, it pops up the message "Windows could not start the Service on Local Computer. For more information,...
8
446
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 cann't be reproduced easily in my system and the call stack generated by the application is given below. Occurrence: 2005/4/22 23:24:57
0
1268
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. The following error has been reported to event viewer sparadically. "Reporting queued error: faulting application PythonService.exe, version 2.3.0.163, faulting module python23.dll, version 2.3.3150.1012, fault address 0x0005c202."
8
3240
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 info for unix person how to survive with python on windows ;-) thanks, jan gregor
3
2606
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 in background as NT service. I got a example code: SmallestService.py from chapter 18 of the book "Python Programming On Win32" by Mark Hammond etc. The code is as follows and runs well as an NT service.
0
2152
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 and calls a C program (enigma.exe) to do the computations. enigma.exe should save its current state when receiving SIGINT or SIGTERM. This (obviously) works under Unix and also when running the script from the Windows command line and...
1
3034
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 install | start | stop The problem is: if I create an exe of this script (all required modules are included in the exe) with gui2exe (a frontend to py2exe) I can install the service - but not start it. The error it returns is "Error
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.