473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

stderr, stdout, and errno 24

To capture output from python scripts run from a C++ app I've added the
following code at the beggening of the C++ app:

PyRun_SimpleString("import grabber");
PyRun_SimpleString("import sys");
PyRun_SimpleString("class a:\n\tdef
write(self,s):\n\t\tograbber.grab(s)\n");
PyRun_SimpleString("import sys\nsys.stderr=a()\nsys.stdout=a()");

Its hard to read that way, here's what it expands to:
import grabber
import sys
class a:
def write(self, s)
grabber.grab(s)

grabber is a C++ extension, the grab function prints displays the
captured text in a Windows app. After running about 450+ scripts in a
row, I get "IOError Errno 24 Too many open files."

I've searched this group and the net and determined that stderr and
stdout may open files, is that correct? If so would each running of a
script be opening new files related to stderr and stdout and not
closing them? I'm just guessing.

Jul 13 '06 #1
5 2582
On 12 Jul 2006 18:09:42 -0700 in comp.lang.python, "Wesley Henwood"
<we***********@hotmail.comwrote:
>To capture output from python scripts run from a C++ app I've added the
following code at the beggening of the C++ app:

PyRun_SimpleString("import grabber");
PyRun_SimpleString("import sys");
PyRun_SimpleString("class a:\n\tdef
write(self,s):\n\t\tograbber.grab(s)\n");
PyRun_SimpleString("import sys\nsys.stderr=a()\nsys.stdout=a()");

Its hard to read that way, here's what it expands to:
import grabber
import sys
class a:
def write(self, s)
grabber.grab(s)
Actually, that last line will more like
ograbber.grab(s)
At least, if what you posted above is accurate...

It's not the question you asked, but if you want to make that easier
to read, you can do something like

PyRun_SimpleString("import grabber");
PyRun_SimpleString("import sys");

PyRun_SimpleString("class a:\n"
" def write(self,s):\n"
" grabber.grab(s)\n");

PyRun_SimpleString("import sys\n"
"sys.stderr=a()\n"
"sys.stdout=a()\n");
C++, like Python, will concatenate strings seperated only by
whitespace.

Regards,
-=Dave

--
Change is inevitable, progress is not.
Jul 13 '06 #2
In article <11**********************@h48g2000cwc.googlegroups .com>,
Wesley Henwood <we***********@hotmail.comwrote:
>To capture output from python scripts run from a C++ app I've added the
following code at the beggening of the C++ app:

PyRun_SimpleString("import grabber");
PyRun_SimpleString("import sys");
PyRun_SimpleString("class a:\n\tdef
write(self,s):\n\t\tograbber.grab(s)\n");
PyRun_SimpleString("import sys\nsys.stderr=a()\nsys.stdout=a()");

Its hard to read that way, here's what it expands to:
import grabber
import sys
class a:
def write(self, s)
grabber.grab(s)

grabber is a C++ extension, the grab function prints displays the
captured text in a Windows app. After running about 450+ scripts in a
row, I get "IOError Errno 24 Too many open files."

I've searched this group and the net and determined that stderr and
stdout may open files, is that correct? If so would each running of a
script be opening new files related to stderr and stdout and not
closing them? I'm just guessing.
I'm guessing, but it sounds like perhaps you're creating an object which has
an open file handle for output for each script that's run. When the
script finishes, if that object still exists, it will keep a file
handle open and eventually you'll hit the system limit on open file
handles for one process.

It's also possible that your C++ app is the one which is failing to
close file handles created for running the scripts - there's no easy
way to tell from the information posted.

You need to examine carefully what happens to any stdin/stdout/stderr
files which are created to execute scripts and ensure that they are
all properly closed (or, in the case of Python, if you don't
explicitly close them, that any references to the files cease to exist
after the script runs). I'd personally recommend explicit closing
here.


--
Jim Segrave (je*@jes-2.demon.nl)

Jul 13 '06 #3
I've checked and double checked my code and I am closing all files
explicitly after opening them. The only possibliy I can think of is
Python opening files each time I run a script, or each time imput to
stderr or stdout is redirected.

Here's a link that is perhaps related to my
problem:<http://pyfaq.infogami.com/why-doesn-t-closing-sys-stdout-stdin-stderr-really-close-it>

Here is a thread in this group, see post by
alisonken1:<http://groups.google.com/group/comp.lang.python/browse_thread/thread/75e65baa1a51b3a6/512bba0739924917?q=too+many+open+files&rnum=20#512 bba0739924917>

Jul 13 '06 #4

Wesley Henwood wrote:
I've checked and double checked my code and I am closing all files
explicitly after opening them. The only possibliy I can think of is
Python opening files each time I run a script, or each time imput to
stderr or stdout is redirected.
<snip>

The problem >I think< is that stout and stderr are not shared with each
invocation.

Since you're calling a python interpreter, the stdout/stderr from the
C++ program is not inherited - so now everytime you call the python
script, it's creating a new process.

With each new process, you're creating a new stdout/stderr handle, and
if you're calling outside of C++ relatively quickly (say more than 100
times a minute), then the old stdout/stderr handles have not had a
chance to be garbage collected - hence, you get too many files open
errors.

The workaround would possibly be to create a Python thread or create a
stdout fifo and a stderr fifo and have your script redirect these
outputs through the fifo buffers that you're C++ code can listen to.

Not sure how to do either one in MS environments, so you'll have to ask
someone else how to work with them.

Jul 13 '06 #5
In article <11*********************@b28g2000cwb.googlegroups. com>,
"Wesley Henwood" <we***********@hotmail.comwrote:
>I've checked and double checked my code and I am closing all files
explicitly after opening them.
If you're running your program under Linux, a very easy way to confirm
this is to look in the directory

/proc/<pid>/fd

where <pidis the PID of your running program. In here you will see a
symlink to every file your program has open, the name of the link being
the file descriptor number.

To make it easier to watch, you may want to stick in a sleep of a few
seconds in-between iterations of the code that executes the Python
script.

If you see the entries piling up in this directory, that will confirm
that you're not closing those files.
Jul 15 '06 #6

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

Similar topics

2
by: Andrei D. | last post by:
Hello Python newsgroup, In the process of developing a big ssh wrapper for sending commands to multiple hosts over the last few months, I (almost accidentally, considering I'm really just an...
6
by: Tsai Li Ming | last post by:
Dear all, I have a problem with a redirecting stdout and stderr. I am a top level module and has no control over the imported modules that are making system calls such as os.system or popen2.* ....
6
by: Farshid Lashkari | last post by:
Hi, My application has python embedded into it. I noticed that when I run any python code the output is buffered and doesn't get flushed until my application exits. To fix this I simply flush...
0
by: Roman Neuhauser | last post by:
Hello, I have a piece of code that gets run in a script that has its stdout closed: import sys sys.stdout = sys.stderr c = subprocess.Popen (..., stdin = subprocess.PIPE,
7
by: Andre | last post by:
Hi, I have a program that sends some output to stdout and some to stderr. I need to separate the two using the command-line so that I direct stderr output to a file, say fileA.txt, and stdout...
0
by: Christoph Haas | last post by:
Evening, I'm having trouble with running a process through Python 2.4's subprocess module. Example code: ======================================================== def run(command): run =...
3
by: Fuzzyman | last post by:
Hello all, Before I ask the question a couple of notes : * This question is for implementing a script inside the Wing IDE. For some reason using the subprocess module doesn't work so I need a...
2
by: Guillaume Dargaud | last post by:
Hello all, a while ago I was pointed towards freopen as a way to redirect stderr to a log file. It works great, but apparently the app also writes a few lines to stdout. Now I could redirect to 2...
4
by: lovecreatesbea... | last post by:
For example, in Bourne Shell both stdout and stderr can be re-directed to /dev/null, $ ./a.out 2>&1 /dev/null then is there any difference still?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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,...

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.