本文介绍了R中的读写pipe()通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数语言都支持双向过程通信.例如,在Python中,我可以(草率地)执行以下操作:

Most languages support two-way process communication. For example, in Python, I can (sloppily) do:

>>> from subprocess import *
>>> p = Popen('nslookup', stdin=PIPE, stdout=PIPE)
>>> p_stdin, p_stdout = p.communicate("www.google.com")
>>> print p_stdin
Server:     ...

在R中,无论我用"r +"还是"w +"打开管道,我似乎只能走一条路.此外,即使我通过R -f ...R < ...运行脚本,在实际的控制台stdin/stdout中也会发生怪异的行为.

In R, I can only seem to go one way, regardless of whether I open my pipe with "r+" or "w+". Furthermore, even if I run a script via R -f ... or R < ..., weird behavior ensues in the actual console stdin/stdout.

我的问题归结为以下内容-是否可以(不编写C方法!)在R中的上述Python示例中重现双向进程通信?

My question boils down to the following - is it possible (without writing a C method!) to reproduce the two-way process communication in the above Python example in R?

推荐答案

很久以前,我还在中使用了双向管道八度所以,是的,这真是太好了.但是,仔细阅读help(pipe)并不表示这是支持.您可以读写,但似乎两者都不是.

A long time ago I also used two-way pipes in Octave so, yes, this would be nice to have. But a perusal of help(pipe) does not suggest that this is support. You get read or write, but seemingly not both.

但是也许你可以作弊.打开管道以写入应用程序,您可以使用标准输出重定向将其调用到文件...,然后继续读取该文件.由于缓冲区未刷新,可能会造成混乱.

But maybe you can cheat. Open a pipe to write into an app which you can call with a stdout redirection to a file ... and then keep reading that file. Could be a mess due to non-flushed buffers though.

这篇关于R中的读写pipe()通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 21:29