If your process is being terminated forcefully (like from the task manager)
then you probably don't want to run any cleanup code. If you really must you
could code something in C, put it in a DLL library and have it executed in
DllMain when the library is unloaded. (And I think you'll still have
problems if someone with enough privileges calls TerminateProcess on your
process).

But what I think you're really looking for is the Service Control Manager:

http://msdn.microsoft.com/en-us/library/ms685150(VS.85).aspx

The ControlService API lets you stop services (among other things):

http://msdn.microsoft.com/en-us/library/ms682108(VS.85).aspx

And the StartService API lets you start a service manually:

http://msdn.microsoft.com/en-us/library/ms686321(VS.85).aspx

Hope this helps! :)
-Mario

On Mon, Jan 11, 2010 at 9:35 PM, Ross Boylan <r...@biostat.ucsf.edu> wrote:

> Is there a way to ensure that some cleanup code will get run whenever my
> process terminates?
>
> I've tried signal and try:finally:, while noting that, e.g., atexit says
> it doesn't handle interrupts.  I can catch many circumstances, but if I
> kill the process from task manager my cleanup doesn't happen (at least,
> print "I'm cleaning up" doesn't come out).
>
> I don't see anything in win32api that looks appropriate, and I don't
> know if Windows provides any sure way to do what I want.  There is a
> call to handle some very abnormal exits, but they are sufficiently bad
> that, like SIGSEGV, I don't want to run the cleanup code (even if I
> can).
>
> The process will eventually run as a service.  I assume there is some
> protocol for managing normal service control manager requests to start
> and stop.
>
> Thanks for any pointers.
> Ross Boylan
>
> _______________________________________________
> python-win32 mailing list
> python-win32@python.org
> http://mail.python.org/mailman/listinfo/python-win32
>



-- 
HONEY: I want to… put some powder on my nose.
GEORGE: Martha, won’t you show her where we keep the euphemism?
_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to