LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2008, 02:16 PM   #1
Law1213
Member
 
Registered: Jul 2004
Location: UK
Distribution: Gentoo
Posts: 52

Rep: Reputation: 17
Unhappy Pipes (in Python)


Hey,

I am currently working on a project in which I am using python to communicate with a c++ program. I need to perform a sequence of reads and writes via pipes.

All the examples I've seen are follow the following structure:-
Open two way pipe
write to pipe
close write end
read from pipe
close read end

I don't want to close the write or read end since I will need to send further data later. If I don't close the write end the program hangs. Is this due to the way pipes work? What is the normal solution in this case, do I need to open a new pipe?

In my case I am using python with the following pipe command
fout, fin = os.popen2("./a.out", "t")

Any help would be much appreciated :-)
 
Old 03-29-2008, 03:24 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by Law1213 View Post
If I don't close the write end the program hangs. Is this due to the way pipes work?
It is because the other program is most likely waiting for input (or an EOF when you close your end). What program is this, and how is its input buffered?

Last edited by osor; 03-29-2008 at 03:25 PM.
 
Old 03-30-2008, 12:24 PM   #3
Law1213
Member
 
Registered: Jul 2004
Location: UK
Distribution: Gentoo
Posts: 52

Original Poster
Rep: Reputation: 17
Smile Problem solved

Yes you were right, one of the processes was waiting for input and so it was hanging. I found that in this situation I needed to use the flush() member function and signal the other process to stop waiting for input.

Not having had much experience with pipes other than the usual console tricks I thought I'd ask about the principles involved for multiple writes and reads. Since there aren't many examples of this.

Python is being used for all the file handling, its then passing this data to a C++ app. Its just some string outputs onto the pipe, another process is reading this data.

Thanks for the input very helpful!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Charming Python: Python elegance and warts, Part 2 LXer Syndicated Linux News 0 08-31-2007 08:40 AM
pipes in python? SciYro Programming 9 08-02-2006 09:35 AM
LXer: Move to python 2.4 / Changing the packaging style for python packages LXer Syndicated Linux News 0 06-13-2006 07:54 PM
python problem - compiled from source - python -V still showing old version txm123 Linux - Newbie 1 02-15-2006 11:05 AM
best practices for subprocs & pipes in python bardinjw Programming 0 11-28-2005 12:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration