Cleanly end a thread with blocked i/o in win32?

Jon Wright jonathan.wright at gmail.com
Fri Oct 22 08:54:00 EDT 2004


elbertlev at hotmail.com (Elbert Lev) wrote in message 
> jonathan.wright at gmail.com (Jon Wright) wrote in message 
>
> > Trying to work around the lack of pexpect for native windows python I
> > had a play with os.popen2 and some threads. This looks promising
> 
> Just curiosity: why did you try using UNIX methods in win32?

I am after a particular functionality - I don't care what methods are
used; I just want to save myself repetitively typing crap into a
program and only answer the interesting questions it asks. Starting
another program and communicating programmatically via stdin and
stdout (usually defined in a language specification) is a convenience
for any multitasking os. Your suggestion helps a great deal however -
one just needed to know the translation into win32 for 'kill'. In my
case this seems sufficient:

def kill_win32(child):
   import win32api, win32con
   child.stdin.close() # should really check if this is open etc
   handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE,
0,[child.pid])
   win32api.CloseHandle(handle)
   child.stdout.close() # ditto
   # error handling

Doubtless other children would resist this and some stronger magic
could be needed to snuff them out?

Perhaps this would be a useful thing to put into the "subprocess.py"
module which I think has now become part of the standard library...?
Probably a windows expert can do much better, and eventually lines
saying if win32 do this, if unix do that. Does that need a PEP or a
patch or is someone going to tell me why it shouldn't be there? Maybe
calling it "child.try_to_kill()" is enough to indicate that it won't
always work?

> To prove that windows isn't unix?

Thanks for your comment. Highly motivating. I now have roughly the
functionality I wanted on both platforms. You hint at the idea that
there is a way to achieve the goal via "win32 methods". I am still
extremely curious to know what these methods might be, I only found
stories that they don't exist in the general case of win32 console io.
Since cygwin python has pty/pexpect modules and runs under win32 the
"proof" fails by contradication?


Jon



More information about the Python-list mailing list