socket's strange behavior with subprocesses
Colin Brown
cbrown at metservice.com
Thu Nov 13 15:04:38 EST 2003
"Jane Austine" <janeaustine50 at hotmail.com> wrote in message
news:ba1e306f.0311121646.70779793 at posting.google.com...
> "Colin Brown" <cbrown at metservice.com> wrote in message
news:<3fb28c56$1 at news.iconz.co.nz>...
> > "Jane Austine" <janeaustine50 at hotmail.com> wrote in message
> > news:ba1e306f.0311120837.6e6eddf2 at posting.google.com...
...
> I tried win32 API primitives for creating subprocesses: win32all's
> CreateProcess. I used the Process class in winprocess.py in the
> "demos" directory. However, it didn't work with sockets perfectly.
>
> Say, a socket server python script is launched via CreateProcess. It
> then launches sub-processes. And I kill(via TerminateProcess) the
> socket server process. The subprocesses remain alive(as expected). I
> try to connect to the 'dead server port'. I expect almost immediate
> "connect refused" error, but it doesn't come up until the subprocesses
> are all gone and client hangs forever.
>
> Jane
If I am interpreting what you are saying here correctly you have:
Main_process
=> [create_process]
=> Sub_process1
socket_server_connection
subprocess2 (of subprocess1) started
Sub_process1 terminated, but socket connection held until subprocess2
terminated.
This is what I would expect based on my findings. Subprocess2 has inherited
the socket handle when it was created (assuming you used os.system,
os.spawn* or os.popen*). You would have to use the correct incantation of
create_process to launch subprocess2.
I have attached the code I used in place of os.system.
Colin
--[Win32.py]---------------------------------------------------------------
# perform equivalent of os.system without open file handles
import win32process,win32event
def system(cmd):
handles = win32process.CreateProcess \
(None,cmd,None,None,0,0,None,None,win32process.STARTUPINFO())
status = win32event.WaitForSingleObject(handles[0],win32event.INFINITE)
if status == win32event.WAIT_ABANDONED:
raise 'win32.system WAIT_ABANDONED'
elif status == win32event.WAIT_FAILED:
raise 'win32.system WAIT_FAILED'
elif status == win32event.WAIT_IO_COMPLETION:
raise 'win32.system WAIT_IO_COMPLETION'
elif status == win32event.WAIT_OBJECT_0:
pass
elif status == win32event.WAIT_TIMEOUT:
raise 'win32.system WAIT_TIMEOUT'
else:
raise 'win32.system - unknown event status = '+str(status)
More information about the Python-list
mailing list