I am developing two app on Windows, one is a GUI using wxPython and the other is like a daemon process. The GUI app will have a start/stop button to control the excution of the daemon. While I know how to use os.popen2() to start my daemon, I can't figure out a way to stop it 'nicely'... Is there anything that I can do?
> I am developing two app on Windows, one is a GUI using wxPython and the > other is like a daemon process. The GUI app will have a start/stop > button to control the excution of the daemon. While I know how to use > os.popen2() to start my daemon, I can't figure out a way to stop it > 'nicely'... Is there anything that I can do?
> Regards,
> -- Wong
You can use the win32process.TerminateProcess function to kill a process... see the win32 docs
"Joe Wong" <joew...@mango.cc> wrote: > I am developing two app on Windows, one is a GUI using wxPython and the > other is like a daemon process. The GUI app will have a start/stop > button to control the excution of the daemon. While I know how to use > os.popen2() to start my daemon, I can't figure out a way to stop it > 'nicely'... Is there anything that I can do?
It depends on your OS. I remember once upon a time, in order to kill processes sucessfully for all Windows platforms (from Win98 to NT to 2000/XP), I had to resort to using tlist.exe and kill.exe, plus w9xpopen.exe for pipes in Windows 98. It was kind of annoying... I seem to remember short name and long names were problem, too. At the end, the killing logic became a small program in itself!
There is an undocumented feature.. UINT uExitCode <--
I don't remember all the details, but I've used it in the past.
if you set the exit code to a certain number (i don't recall what but it is possibly -1, 0, or 1... maybe 2 or -2) then the OS will force a 'shut' beyond the normal call to terminate process.
When the OS is 'shutting down' it calls TerminateProcess on each process passing that value. This trick prevents processes from restarting (if you have a process like explorer that you are trying to kill).
Since I don't recall the details, it would take a bit of experimentation. But hopefully not too much.
David ===================== http://cellphone.duneram.com "Surf a wave to the future" I'm a windows SDK CBT guru with a patent....
>From: hungjun...@yahoo.com (Hung Jung Lu) >To: python-l...@python.org >Subject: Re: how to terminate a process on win32? >Date: 24 May 2004 15:11:49 -0700
>"Joe Wong" <joew...@mango.cc> wrote: > > I am developing two app on Windows, one is a GUI using wxPython and the > > other is like a daemon process. The GUI app will have a start/stop > > button to control the excution of the daemon. While I know how to use > > os.popen2() to start my daemon, I can't figure out a way to stop it > > 'nicely'... Is there anything that I can do?
>It depends on your OS. I remember once upon a time, in order to kill >processes sucessfully for all Windows platforms (from Win98 to NT to >2000/XP), I had to resort to using tlist.exe and kill.exe, plus >w9xpopen.exe for pipes in Windows 98. It was kind of annoying... I >seem to remember short name and long names were problem, too. At the >end, the killing logic became a small program in itself!
But calling TerminateProcess will stop the application right away. I need someway that the process being killed get notified and thus to carry out some procedure before shut down. On Linux, I can use kill(pid, signum) to achive this but on windows?
> But calling TerminateProcess will stop the application right away. I need > someway that the process being killed get notified and thus to carry out > some procedure before shut down. On Linux, I can use kill(pid, signum) to > achive this but on windows?
There is no way guaranteed to work on Windows. This MS article explains the *intended* way, but many apps (and especially console apps) don't play this game:
The short course is that native Win32 programs with a GUI "should" be running a message pump, and their top-level window should respond to a WM_CLOSE message by shutting down the app cleanly. If they're not coded that way, then no, you have in general no way to force them to shut down cleanly. Googling will turn up many approximations that more-or-less work for different kinds of apps. Bottom line is really that life is sheer hell unless the process you're trying to shut down *advertises* a way to force that from outside (like responding to WM_CLOSE, or the state of a global semaphore, or a particular string of bytes sent to an agreed-upon socket, etc).