473,696 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

socket's strange behavior with subprocesses

Running Python 2.3 on Win XP

It seems like socket is working interdependentl y with subprocesses of
the process which created socket.

------------------------------------
#the server side
import socket
s=socket.socket (socket.AF_INET ,socket.SOCK_ST REAM)
s.bind(('localh ost',9000))
s.listen(5)
z=s.accept()
import os
f=os.popen('not epad.exe') #notepad appears on the screen
z[0] <socket._socket object object at 0x0096C390> z[0].recv(6) 'foobar' z[0].send('hello world') 11

#the client side s=socket.socket (socket.AF_INET ,socket.SOCK_ST REAM)
s.connect(('loc alhost',9000))
s.send('foobar' ) 4 print s.recv(1024) hello world

------------------------------
Now when the client requests to recv 1024 bytes, and since there is no
more to read from the socket it blocks.

#client side s.recv(1024) #it hangs
and the server side tries to close the socket:

#server side z[0].close()
#yes, it seems to have worked.


Alas, the client side doesn't wake up! It doesn't wake up unless the
notepad is exited first; only after that, 'Connection reset by peer'
is raised. What does the socket has to do with subprocesses?
Jul 18 '05 #1
4 3362
Jane Austine wrote:
and the server side tries to close the socket:

#server side
z[0].close()
#yes, it seems to have worked.

Alas, the client side doesn't wake up! It doesn't wake up unless the
notepad is exited first; only after that, 'Connection reset by peer'
is raised. What does the socket has to do with subprocesses?


Nothing, I guess... try to shutdown the socket explicitly before closing it:
z[0].shutdown(2)
z[0].close()

does that work?

--Irmen

Jul 18 '05 #2
"Jane Austine" <ja***********@ hotmail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
Running Python 2.3 on Win XP

It seems like socket is working interdependentl y with subprocesses of
the process which created socket.
and the server side tries to close the socket: ...... Alas, the client side doesn't wake up! It doesn't wake up unless the
notepad is exited first; only after that, 'Connection reset by peer'
is raised. What does the socket has to do with subprocesses?


Hi Jane

I think it may be tied up with the way subprocesses are made under Windows.
Below is a copy of part of a post I made some months back when inexplicable
file-locking was causing me problems. It took me weeks to track down the
reason.

The Python default subprocess inherits open handles from the parent.
Your options are:
1. Open the subprocess before the socket.
2. Create the subprocess using win32 API primitives (this is what I had to
do).
Let me know if want details.

Colin Brown
PyNZ

----------------------------------------------------------------------------
------
I have been struggling to solve why an occasional "Permission Denied" error
popped up from the following code fragment in one of my Win2K program
threads:

....
input_file = preprocess(raw_ file)
os.system('thir d_party input_file output_file > error_file')
if os.path.exists( saved_file):
os.remove(saved _file)
os.rename(input _file,saved_fil e)
....

The error occurs on the os.rename(). The third_party executable was closing
input_file before terminating and anyway the subshell process has finished
before the os.rename is called! Most baffling.

With the aid of handle.exe from www.sysinternals.com I finally resolved the
problem. To see the problem at first hand try the following script:

import time, thread, os

def rm(file):
print 'delete x.x'
os.remove(file)

def wait():
if os.name == 'nt':
os.system('paus e')
elif os.name == 'posix':
os.system('slee p 3')
print 'end wait'

print 'create x.x'
f = open('x.x','w')
thread.start_ne w_thread(wait,( ))
time.sleep(1)
print '\nclose x.x'
f.close()
rm('x.x')

Although this works fine on Linux, I get a Permission Denied error on
Windows. I surmise that an os.system call uses a C fork to create the
subshell - an exact duplicate of the current process environment including
OPEN FILE HANDLES. Because the os.system call has not returned before the
os.remove is called a "Permission Denied" error occurs. Quite simple really.

Now going back to my original problem, I had other threads doing os.system
calls. When one of these calls happens during the preprocess file write of
my above thread AND takes longer to complete than the os.system call in the
above thread then the file will still be open and the os.rename will return
the Permission Denied error.

Jul 18 '05 #3
"Colin Brown" <cb****@metserv ice.com> wrote in message news:<3f******* *@news.iconz.co .nz>...
"Jane Austine" <ja***********@ hotmail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
Running Python 2.3 on Win XP

It seems like socket is working interdependentl y with subprocesses of
the process which created socket.
and the server side tries to close the socket:

.....
Alas, the client side doesn't wake up! It doesn't wake up unless the
notepad is exited first; only after that, 'Connection reset by peer'
is raised. What does the socket has to do with subprocesses?


Hi Jane

I think it may be tied up with the way subprocesses are made under Windows.
Below is a copy of part of a post I made some months back when inexplicable
file-locking was causing me problems. It took me weeks to track down the
reason.

The Python default subprocess inherits open handles from the parent.
Your options are:
1. Open the subprocess before the socket.
2. Create the subprocess using win32 API primitives (this is what I had to
do).
Let me know if want details.

Colin Brown
PyNZ


Thank you very much, first of all.

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 TerminateProces s) 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
Jul 18 '05 #4

"Jane Austine" <ja***********@ hotmail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.com...
"Colin Brown" <cb****@metserv ice.com> wrote in message news:<3f******* *@news.iconz.co .nz>...
"Jane Austine" <ja***********@ hotmail.com> wrote in message
news:ba******** *************** ***@posting.goo gle.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 TerminateProces s) 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_c onnection
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,wi n32event

def system(cmd):
handles = win32process.Cr eateProcess \
(None,cmd,None, None,0,0,None,N one,win32proces s.STARTUPINFO() )
status = win32event.Wait ForSingleObject (handles[0],win32event.INF INITE)
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_COMPLET ION'
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)

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2436
by: Marcos | last post by:
Hi guys, I realise this question has been answered in one form or another many times before but I can't quite find the solution I need. I am trying to run multiple subprocesses from a python script and then wait until all subprocesses have completed before continuing. The subprocesses run on other machines ie this is a coarse grained parallel computation task. So the commands I am using are: for x in range(0, limit): os.system("xgrid...
5
11689
by: Terry | last post by:
It's my understanding of UDP sockets that if there is a thread blocked on a "recvFrom()" call and other thread sends a UDP packet to some address, that if the machine on the other end isn't up, that the "recvFrom()" call should just continue blocking. Right? What I'm seeing is that when I send a packet to a particular address that is not responding, my "recvFrom()" call throws an exception. I get "An existing connection was forcibly...
10
5299
by: feel52 | last post by:
Below you'll find the code i'm working on. It's in a button click routine and hangs after 3 or 4 sometimes 5 loops done, probably in sock.receive(....). Some code was found here( on google i mean) but the amazing( at least for me) is that if i run the program step by step or i cancel the loop and run it by pressing the button again and again it never hangs . I want to have an infinite loop so my socket keeps on listening until i...
1
3389
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows XP. About the architecture: I have a socket server dll that contains a class that handles connections for a given local ipaddress and port. This class(server) can be started or stopped by calls to the appropriate functions. The server class has...
0
1911
by: richard | last post by:
OS: Winxp and Win2003 Visual Basic.NET 2003 MS-SQL Server 2000 hey all I am a newbie in vb.net but i have managed to build a simple chat server in vb.net using socket and a client connecting to it made in vb6. Some of the code in the server are based on the basic samples that i get from other authors. To give you an idea, the server uses sockets,
1
3864
by: richard | last post by:
OS: Winxp and Win2003 Visual Basic.NET 2003 MS-SQL Server 2000 hey all I am a newbie in vb.net but i have managed to build a simple chat server in vb.net using socket and a client connecting to it made in vb6. Some of the code in the server are based on the basic samples that i get from other authors. To give you an idea, the server uses sockets,
2
2463
by: ne.seri | last post by:
In short, I'm building a kind of server which is supposed to handle open connections with clients. E.g. client connects to the server, the connection stays open, client sends a request to the server, server reasoned, and so on... The thing is that connection MUST always stay open. I'm using C++, and as for sockets, I'm using native syscalls, not MFC. I'm using stream sockets for communication. Another thing is that is should work both...
9
2151
by: Irmen de Jong | last post by:
Hi, Recently I was bitten by an apparent bug in the BSD socket layer on Open VMS. Specifically, it appears that VMS defines MSG_WAITALL in socket.h but does not implement it (it is not in the documentation). And I use the socket.MSG_WAITALL flag on my recv() calls... and then they crash on OpenVMS. I don't have access to an OpenVMS machine myself so could someone else that has (or has more knowledge of it) shed some light on it?
6
4645
by: White Spirit | last post by:
I have the following code to send a packet to a remote socket and receive a response in return: System.Net.Sockets.Socket locSocket = new System.Net.Sockets.Socket (AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp); System.Net.Sockets.Socket remSocket = new System.Net.Sockets.Socket (AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Tcp); locSocket.Bind (new IPEndPoint (locIP, 2126)); /*
0
8656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8590
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9140
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8869
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7693
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2304
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.