关于StringIO的问题 [英] Question about StringIO

查看:56
本文介绍了关于StringIO的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好


我明白StringIO在内存中创建了一个类似文件的对象。


是否可以调用另一个程序,使用os.system()或

os.popen(),并使用<重定向操作符,以便其他程序

读取我的StringIO对象作为其输入?


如果需要,我会提供更多详细信息,但希望这已经足够了

表示简单的是或否答案,如果是,如何。


BTW,我尝试过使用popen2()并通过stdin传递我的数据,但是

其他程序(psql)对此没有反应 - 再次,如果需要,我会给

更多信息。


谢谢


Frank Millman

解决方案

Frank Millman写道:

大家好

我知道StringIO在内存中创建了一个类似文件的对象。

是否可以使用os.system()或<调用另一个程序os.popen(),并使用<重定向操作符,以便其他程序读取我的StringIO对象作为其输入?

如果需要,我会提供更多详细信息,但希望这对于简单的是或者这是足够的没有答案,如果是的话,怎么样。

顺便说一句,我已经尝试过使用popen2()并通过stdin传递我的数据,但
其他程序(psql)对此没有反应很好 - 再次,如果有必要,我会给出更多信息。



不幸的是,StringIO模块只在它们被调用的

过程中创建实例:这些

操作系统或其他进程没有对象,因此不能用于进行进程间通信。

问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC www.holdenweb.com

PyCon TX 2006 www.python.org/pycon/


Frank Millman写道:

大家好

我知道StringIO在内存中创建了一个类似文件的对象。
<是否可以使用os.system()或
os.popen()来调用另一个程序,并使用<重定向操作符,以便其他程序读取我的StringIO对象作为其输入?


编号流程不共享内存 - 因此你必须使用临时的

文件或管道。

顺便说一句,我尝试过使用popen2()并通过stdin传递我的数据,但
其他程序(psql)对此没有反应 - 再次,如果需要,我会给予
更多信息。 / blockquote>


最好这样做:)


Diez




Diez B. Roggisch写道:

Frank Millman写道:

大家好

我明白StringIO创建了一个文件 - 像内存中的对象一样。

是否可以使用os.system()或os.popen()来调用另一个程序,并使用<重定向操作符,以便其他程序读取我的StringIO对象作为其输入?



否。进程不共享内存 - 因此你必须使用temp
文件或管道。

BTW,我尝试过使用popen2()并通过stdin传递我的数据,但其他程序(psql)没有对此反应良好 - 如果有必要,我会给予更多信息。



最好这样做:)

Diez




谢谢Steve和Diez的回复。我不认为这是可能的,但是值得问一下:-)


我将尝试用popen()简要解释一下我的经验。


我有一些sql脚本来创建表,索引,程序等。

目前有大约50个脚本,但这个数字会增长。到目前为止,我已经手动运行了
。现在我想自动完成这个过程。


我支持PostgreSQL和MS SQL Server,语法是

在某些情况下略有不同。我没有维护两组

脚本,而是使用-pg-或-ms-为一些行添加前缀来表示

平台,然后使用Python来解析脚本和为每个平台生成一个

正确的输出,使用popen()分别将它传递给''psql''和''osql''




我遇到了一些问题,但描述它们需要很长时间

all,我只想要一个有效的解决方案,所以我将专注于我最新的/>
尝试。


我运行所有脚本并使用我想传递的

字符串创建一个StringIO对象。它长约250 000字节。如果我使用popen()运行psql

,并通过stdin将字符串传递给它,它工作正常,但我

获取屏幕上的所有消息。如果我这样做,但用''>结束

命令fjm 2>& 1''它正常工作,并且文件fjm中的消息结束了

,大约40 000字节长。如果我用

popen4()来运行它,它就会启动,但是大约会挂起1/4。

在MSW上完全相同。它似乎对stdout文件的

大小有限制 - 这可能吗?


为了我的目的,我很乐意使用popen ()和选择没有

重定向,重定向到文件或重定向到/ dev / null。关于popen4()的问题

因此是学术性的,虽然我有兴趣

知道答案。


BTW,在MSW上有/ dev / null的等价物吗?


提前感谢任何建议。


Frank


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?

I will provide more details if required, but hopefully this is enough
for a simple yes or no answer, and if so, how.

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.

Thanks

Frank Millman

解决方案

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?

I will provide more details if required, but hopefully this is enough
for a simple yes or no answer, and if so, how.

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.


Unfortunately the StringIO module only creates instances inside the
process they are called: these objects have no existence to the
operating system or to other processes, and so can''t be used for
inter-process communication.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/


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



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


这篇关于关于StringIO的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆