Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

stopping a python windows service

56 views
Skip to first unread message

DK

unread,
Aug 16, 2005, 6:12:00 PM8/16/05
to
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.

Benjamin Niemann

unread,
Aug 16, 2005, 6:25:38 PM8/16/05
to
DK wrote:

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.isset():
pollWebsite()

for i in xrange(1800):
if shutdownEvent.isset():
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.isset() at certain intervals - instead of
urlopen which just blocks.


--
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/

Grig Gheorghiu

unread,
Aug 16, 2005, 6:24:00 PM8/16/05
to
Here are 2 recipes from the online Python Cookbook. I've used this one
very successfully:
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/115875>.

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

Grig

Do Re Mi chel La Si Do

unread,
Aug 16, 2005, 7:12:10 PM8/16/05
to
Hi !

Use SC.exe (windows-XP) (with popen ?)
For help : sc /?


You can, also, try :
qprocess /?
tasklist /?
taskkill /?
etc.

@-salutations

Michel Claveau

DK

unread,
Aug 17, 2005, 9:20:46 AM8/17/05
to
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...

Message has been deleted

Peter Hansen

unread,
Aug 17, 2005, 10:35:54 PM8/17/05
to
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.isSet() instead.

-Peter

0 new messages