-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
os.kill on windows #42083
Comments
This patch enables os.kill on windows. This way there Basically kill calls TerminateProcess and the "signal" Don't have CVS access today so I'm sending the whole Also some config wizard need to change the winsows |
Logged In: YES Converted to unified diff. |
Logged In: YES So is there a reason this isn't in the latest Python? The |
Logged In: YES Karl, can you test this patch and verify it works? It would |
Logged In: YES As is, the patch has two major problems:
Furthermore, this patch would duplicate So in its current form, I think the patch should be rejected. |
Logged In: YES IIRC (the patch was done a long time ago), Python uses the I agree that the patch need more work, mainly the error At least on Python 2.4.3, TerminateProcess is not exposed by I still think that having a way to kill a process OOTB on |
Logged In: YES "Python uses the process handle as process id" makes no real |
Logged In: YES Martin, FYI, on Windows the os.spawn() functions return the |
Logged In: YES Ah, ok. Given that _subprocess already offers that |
Logged In: YES Well, os.waitpid() on Windows also takes a process handle. |
This should be implemented differently. Users should substitute their |
The Popen(...).terminate() works only if I'm the one who started the |
Yes, this feature would be very important for me too... Anybody knows, _when_ it will be integrated into Python ? If this bugzilla supports email notifications, Please add me as "CC" for -Technologov |
It would be really useful to be able to send signal.SIGINT to processes on Windows using os.kill(...). The patch as described sounds like it would have a different signature to the standard implementation of os.kill(...) which takes a pid and a signal type. IronPython 2.7 will have an os.kill implementation. Looks like it only supports signal.SIGINT and signal.SIGBREAK and just calls:
|
I'm wrong. First IronPython tries: NativeSignal.GenerateConsoleCtrlEvent(PythonSignal.CTRL_C_EVENT, ...) But with the comment: //The following calls to GenerateConsoleCtrlEvent will fail under It falls back to just killing the process. |
Aaaand the IronPython implementation of NativeSignal.GenerateConsoleCtrlEvent(PythonSignal.CTRL_C_EVENT, ...) |
To make it clear, even though it would be incomplete, a partial implementation of os.kill(...) for Windows would be very useful and provide some level of compatibility with applications that use os.kill (so even if os.kill(...) duplicates functionality in other modules - although that was disputed - it should be provided for compatibility reasons). An implementation similar to the IronPython one is probably the best that can be managed and would still be useful: accepting only signal.SIGINT and signal.SIGBREAK and first trying Kernel32.GenerateConsoleCtrlEvent, falling back to killing the process. |
So, Michael, what do you think: should os.kill expect a process handle, or a process ID? |
Hmm... well my particular use case is that it should work with the value returned by os.getpid(). If that is a process handle then it is nice and convenient to just use process handles. The docs don't specify so I bet it returns a pid. :-( However Brian Curtin might want to weigh in on this as he has been looking at implementing this and there might be constraints. |
I have this working with process IDs and my vote would be to keep it that way, as it would stay in-line with the other platforms, and it seems to work so far. I would imagine that was also IronPython's goal. I'm still working on the test portion of the patch. It should be ready shortly. |
Here is a patch with some tests and doc changes. I'm having trouble coming up with tests which will work with CTRL_C_EVENT and CTRL_BREAK_EVENT. Based on my understanding of GenerateConsoleCtrlEvent, I figured this example (http://msdn.microsoft.com/en-us/library/ms685049%28v=VS.85%29.aspx) would be receptive to CTRL+C when started as a subprocess, but it doesn't work. I also figured "python -c "input()"" started as a subprocess could be killed with CTRL_C_EVENT, but it doesn't work either. |
Removed an unnecessary goto and fixed a few tab/space inconsistencies (ugh). |
After discussion with Brian it seems like it should be possible for os.kill(...) on Windows to support both pids *and* process handles. This seems ideal. |
Michael, do you have an example of something which returns a handle? This current patch doesn't work with handles, but it wouldn't be hard to add it. I could make it work with the _handle object of a Popen object, but you could just as easily call os.kill on the pid of the Popen object. I don't know of any Python-wide handle object...I know subprocess has it's own, and don't really know what other functions are returning handles. Anyways, this patch includes an additional test script which uses ctypes to setup a console control handler, and CTRL_BREAK_EVENT is tested successfully. However, CTRL_C_EVENT is not. See http://msdn.microsoft.com/en-us/library/ms686016%28v=VS.85%29.aspx for details, but I'm not able to get the subprocess to work with CTRL+C. Calling SetConsoleCtrlHandler(NULL, FALSE) either in the script, test_os, or in _subprocess.c does not change anything, although it seems that it should. The CTRL_C_EVENT test is currently skipped in the patch until I can figure that out. |
According to earlier discussion in this issue os.spawn() return process handles on Windows. |
Committed to trunk in r79633 after talking with Michael about it. I'll forward port it after the 2.7 beta goes out. |
FAQ should be updated http://www.python.org/doc/faq/windows/#how-do-i-emulate-os-kill-in-windows |
How about something like this patch? |
Seems good to me, even though I'd rewrite some parts like this:
... In newer Python versions :func:`os.kill` works on Windows with the ...anatoly t. On Mon, Apr 5, 2010 at 6:38 PM, Brian Curtin <report@bugs.python.org> wrote:
|
Ported to py3k in r80008. FAQ text updated in r80009 and r80010. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: