Question about StringIO

Benjamin Niemann pink at odahoda.de
Mon Oct 10 14:24:59 CEST 2005


Frank Millman wrote:

> I will try to explain my experience with popen() briefly.
> 
> I have some sql scripts to create tables, indexes, procedures, etc. At
> present there are about 50 scripts, but this number will grow. I have
> been running them manually so far. Now I want to automate the process.
> 
> I am supporting PostgreSQL and MS SQL Server, and the syntax is
> slightly different in some cases. Rather than maintain two sets of
> scripts, I prefix some lines with -pg- or -ms- to indicate the
> platform, and then use Python to parse the scripts and generate a
> correct output for each platform, passing it to 'psql' and 'osql'
> respectively, using popen().
> 
> I have had a few problems, but it would take too long to describe them
> all, and I just want a working solution, so I will focus on my latest
> attempt.
> 
> I run through all the scripts and create a StringIO object with the
> string I want to pass. It is about 250 000 bytes long. If I run psql
> using popen(), and pass it the string via stdin, it works fine, but I
> get all the messages on the screen. If I do the same, but end the
> command with ' > fjm 2>&1' it works correctly and the messages end up
> in the file fjm, which is about 40 000 bytes long. If I run it with
> popen4(), it starts ok, but then hangs about 1/4 of the way through.
> Exactly the same happens on MSW. It seems to be hitting a limit on the
> size of the stdout file - is that possible?
> 
> For my purposes, I will be happy to use popen() and a choice of no
> redirection, redirect to a file, or redirect to /dev/null. The question
> about popen4() is therefore academic, though I would be interested to
> know the answer.

That's probably a deadlock as described in
<http://docs.python.org/lib/popen2-flow-control.html>

> BTW, is there an equivalent of /dev/null on MSW?

Dunno - but as a last resort, you could create a tempfile with a unique name
(to be sure, not to override any existing data), dump your output there and
later os.unlink() it...

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/



More information about the Python-list mailing list