473,507 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

does Popen.communicate() close all parent-side file descriptors?

Hi,

I am brand-new to Python but am an experienced C/Unix programmer. I am rewriting in Python some old shell scripts that do lots of stuff like

set output = `cat this | grep that | whatever ...`

My sense is that this should be done using subprocess.Popen() since, although I am using Python 2.4 (Linux) and 2.6 (MacOS), all of the other library interfaces for this sort of thing are being deprecated/removed in future releases.

I have found a number of examples that show how to do this, including a very appropriate one on Doug Hellmann's website that shows how to chain a bunch of Popen()s together to mimic the pipeline example shown above. I have three questions, however:

(1) Does Popen.communicate() close all parent-side file descriptors opened by the Popen() call when the child exits? I am connecting the child's stdout to the parent, e.g.,

comm= subprocess.Popen(args, stdout=subprocess.PIPE)

This is being done in a loop run thousands of times so I need to make sure that each file descriptor opened in the parent to communicate with a child is closed as soon as I am finished with it.

(2) If I do not use Popen.communicate() (or if it does not close file descriptors), exactly which file descriptors do I need to close and how? I would assume the only things I need to close are any of the stdio streams for which I specified =PIPE in the Popen() call. For the above example would I just do

comm.stdout.close()

and not worry about anything else?

(3) Documentation on Popen.returncode is a bit vague. Is this the child's exit code only or is it the combination of exit code and signal status that is more fully documented in os.wait()?

Thanks!
Oct 13 '10 #1
3 3473
dwblas
626 Recognized Expert Contributor
Python's closing of file handles is spotty. I ran into the same problem while using a class' __del__ method to close files. Python closes Python objects, and a file pointer/handle is not a Python object so it gets garbage collected but when is not necessarily known. I would say to use the Python equivalent of
`cat this | grep that | whatever ...`
instead of invoking a bash shell.
Expand|Select|Wrap|Line Numbers
  1. for fname in os.listdir(path_name):
  2.     fp = open(os.path.join(path_name, fname)):
  3.     for rec in fp:
  4.         if search_string in rec:
  5.             print rec
  6.     fp.close() 
Oct 14 '10 #2
Thanks for the reply, dwblas. I actually provided a poor example (`cat this | grep that | whatever`), there are many instances where I need to communicate with external programs to do things that Python cannot do internally.

I received a pretty comprehensive answer to this question from Chris Torek over on comp.lang.python who apparently researched the subprocess code before responding. According to him these parent-side pipe file descriptors are explicitly closed by Popen.communicate(). In cases where that routine is not used (and I have several), Popen()'s delete method will close them when it is called by the garbage collector. This seems borne out by my own testing, although since GC is unreliable I think I will be explicitly calling Popen.stdXXX.close() myself whenever I am done with the pipe on the parent side.

Thanks!
Oct 14 '10 #3
dwblas
626 Recognized Expert Contributor
Just wanted to make you aware of pexpect
Oct 14 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
3622
by: J Peterman | last post by:
I need to do this exercise, but am having problems. I need to write a program that firstly, sleeps for 5 seconds, then reads a line of input from file descriptor 0 and then writes the line back...
5
3541
by: JG | last post by:
Hi all, Does anyone know how the implementations on Linux and Windows handle synchronization between a read and write FD open to the same file. For example, if I have 2 FD open to file X.txt. ...
1
2054
by: upendrao | last post by:
Hi, I have a requirement in which I need to count the number of file descriptors associated with a file. To be more clear...A file can be opened by several processes say in read only mode. I would...
4
5791
by: Joe Lester | last post by:
I'm trying to figure out what the optimal Postgres configuration would be for my server (with 200 connecting clients, even though I'd really like to get it up to 500). I've got a 700 MHz eMac...
0
1058
by: Didier Trosset | last post by:
I am using the following code. It is used to spawn a new process (using popen) and to change the file handles so that subsequent writes to standard output with printf goes into this child process...
5
1995
by: xhy_China | last post by:
How to close an file in an running process from another process? For example: There are two running processes,named procIn and procFrom. A file named needCloseFile was opened in procIn. Now I...
0
1368
by: jfigueiras | last post by:
>I have a problem with the module subprocess! As many other programs... I'm not sure what you mean by "non-standard file descriptors". The other program is free to open, read, write, etc any...
3
5248
by: Yang | last post by:
Hi, I'm experiencing a problem when trying to close the file descriptor for a socket, creating another socket, and then closing the file descriptor for that second socket. I can't tell if my issue...
2
5402
by: DJ Dharme | last post by:
Hi all, I am writing a multi-threaded application in c++ running on solaris. I have a file which is updated by a single thread by appending data into the file and at same time the other threads...
0
7223
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,...
0
7111
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...
0
7319
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,...
0
7376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
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,...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1542
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.