Question about StringIO

Frank Millman frank at chagford.com
Mon Oct 10 06:30:55 EDT 2005


Diez B. Roggisch wrote:
> Frank Millman wrote:
> > Hi all
> >
> > I understand that StringIO creates a file-like object in memory.
> >
> > Is it possible to invoke another program, using os.system() or
> > os.popen(), and use the < redirect operator, so that the other program
> > reads my StringIO object as its input?
>
> No. Processes don't share memory - thus you have to either use a temp
> file, or pipes.
>
> > BTW, I have tried using popen2() and passing my data via stdin, but the
> > other program (psql) does not react well to this - again, I will give
> > more info if necessary.
>
> Better do so :)
>
> Diez

Thanks, Steve and Diez, for the replies. I didn't think it was
possible, but it was worth asking :-)

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.

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

Thanks in advance for any suggestions.

Frank




More information about the Python-list mailing list