473,574 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stopping a python windows service

DK
i was able to successfully create a windows service using py2exe. it
polls a website periodically and logs it to a file. this is done using
a function that does an infinite loop with periodic "sleeps".

my question is...

what's the best way to stop this service gracefully?

when try to stop it from the services applet from control panel, it
takes forever and then gives me an error.

currently, the only way i am able to stop it is using the task manager
and killing the process.

Aug 16 '05 #1
6 5081
DK wrote:
i was able to successfully create a windows service using py2exe. it
polls a website periodically and logs it to a file. this is done using
a function that does an infinite loop with periodic "sleeps".

my question is...

what's the best way to stop this service gracefully?

when try to stop it from the services applet from control panel, it
takes forever and then gives me an error.

currently, the only way i am able to stop it is using the task manager
and killing the process.


Windows services generally use two threads: one to do the work and one to
listen for messages from the
whatever-the-component-is-called-to-control-services.
When the message thread received a 'stop' message, it should inform the
worker thread to shut down, e.g. using threading.Event . So your worker
should regularily check for the shutdown event, e.g.:

while not shutdownEvent.i sset():
pollWebsite()

for i in xrange(1800):
if shutdownEvent.i sset():
break
time.sleep(1)

But if you get the 'stop' message while the worker thread is in
pollWebsite() and the webserver is sloooow, you'll still have a significant
delay... To avoid this, you would need a http client based on select() that
allows you to check shutdownEvent.i sset() at certain intervals - instead of
urlopen which just blocks.
--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
Aug 16 '05 #2
Here are 2 recipes from the online Python Cookbook. I've used this one
very successfully:
<http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/115875>.

This one seems simpler:
<http://aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/59872>

Grig

Aug 16 '05 #3
Hi !

Use SC.exe (windows-XP) (with popen ?)
For help : sc /?
You can, also, try :
qprocess /?
tasklist /?
taskkill /?
etc.

@-salutations

Michel Claveau

Aug 16 '05 #4
DK
I may have taken your code example too literally. I tried putting in
the check for 'shutdownEvent. isset()' but it's failing at run time.
It's looking for a global variable, I guess.

Do I have to register these threads somehow in the beginning?

I'm somewhat new to Python so please be patient...

Aug 17 '05 #5
On 17 Aug 2005 06:20:46 -0700, "DK" <pr*****@gmail. com> declaimed the
following in comp.lang.pytho n:
I may have taken your code example too literally. I tried putting in
the check for 'shutdownEvent. isset()' but it's failing at run time.
It's looking for a global variable, I guess.
"shutdownEv ent" is an event instance created using the threading
module.
Do I have to register these threads somehow in the beginning?
You'll have to create an Event object, a worker thread, and perhaps
leave the main thread as the monitor for system shutdown events.
I'm somewhat new to Python so please be patient...
Why do I find it discomforting that people new to the language keep
wanting to do deep OS specific actions in their first week?

{Granted, my first program was a rudimentary "sendmail" for my Amiga,
using ARexx scripts from the email client to queue messages into a temp
directory, and waking up, if needed, the Python program to parse the
to:, cc:, and bcc: headers, handshaking with the ISP's outgoing daemon
-- but everything I wrote was native Python; no OS specific code.
I needed to do this as the downloadable "sendmails" had major flaws:
the first one created a message file for each recipient, and would get
stuck if one of the recipient addresses wasn't receiving (this was back
in the days before ISPs locked port 25 passthrough); the second relayed
via the ISP -- but did not handshake the cc/bcc addresses, so such never
received the mail.}
-- =============== =============== =============== =============== == <
wl*****@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Aug 17 '05 #6
DK wrote:
I may have taken your code example too literally. I tried putting in
the check for 'shutdownEvent. isset()' but it's failing at run time.
It's looking for a global variable, I guess.


Or perhaps "it" is just looking for correct capitalization, since Python
is case sensitive. Try shutdownEvent.i sSet() instead.

-Peter
Aug 18 '05 #7

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

Similar topics

5
2592
by: CG | last post by:
Hi I have developed a Windows Service When I try to start the Service it tells me that it cannot start as there may not be any work to do When I comment out below line of code in my OnStart event.... objEventLog.EnableRaisingEvents = Tru ....It starts fin I have the same issue when stopping the service because of below line of code in...
0
1530
by: Daniel O'Brien | last post by:
Hi - any help with this would be greatly appreicated - it has already had me confused for a good few hours! I am using Visual Studio 2003 and the .NET framework 1.1. I have a C# Windows Service, with my own class extending from ServiceBase in the normal fashion. The service starts a remoting object (WKO and singleton) which many clients...
4
15874
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group : microsoft.public.dotnet.languages.csharp URL :...
0
1046
by: GTS | last post by:
Hi, I have created my application to run as windows service. It is developed using VC++ on Windows 2000 platform. It is working fine with Windows 2000. But I am seeing problems in Windows 2003 while stopping the service. Is there any specific change needed to run it on Windows 2003. Thanks
7
10045
by: Gene | last post by:
I have a Windows Service (VB.NET) that runs 24/7. It queries a Web service 5 to 10 times per hour. About 2 or 3 times a month, it fails. The log indicates that it sends the request to the Web service but there is no return from the service. Stopping and restarting the Windows service cures the problem. Our desire is to cure the problem...
0
1420
by: Glen Wolinsky | last post by:
I am creating a Windows service that will check a request queue (database) for pending requests. It will then process each individual request until completed, wait a set time interval and then move on to the next one in the list. The processing routine could take 1-2 minutes to finish processing one request. My question is this: If the...
5
17402
by: chris.hearson | last post by:
How do I programmatically prevent a service from stopping? I want to be able to keep my service running (started), under certain conditions, when a user tries to stop it. I have tried throwing an exception - from OnStop(): the SCM waits for a long time, reports that the service didn't respond in a timely fashion and then leaves the status...
6
8045
by: D | last post by:
I have a simple file server utility that I wish to configure as a Windows service - using the examples of the Python Win32 book, I configured a class for the service, along with the main class functions __init__, SvcStop, and SvcDoRun (which contains my server code). After registering the service, I am able to start it with no problems....
10
7510
by: archana | last post by:
Hi all, I am having one windows service which is updating to database. On 'Onstop i want to wait till current updation complete. How will i do this? Because if i write some lengthy code on onstop it fails to stop and status remain 'stopping' Later i can't do anything on that service even i can't uninstall that service.
0
7794
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...
0
7710
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8040
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8225
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7800
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...
0
3738
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...
0
3745
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2230
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1051
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...

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.