Chris Culnane
2018-Sep-12 07:31 UTC
[Rd] Bug 17432 in readLines with R >= 3.5.0 still a problem
Bug 17432 (https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17432) is still a problem when using pipes for IPC. The bug is evident when calling R from another process and trying to communicate via StdIn. R will buffer the input and not read lines until the buffer is exceeded or StdIn is closed by the sending process. This prevents interactive communication between a calling process and a child R process.>From a quick look at the source code, it looks like the bug is caused by only disabling buffering when isatty() returns true for a file descriptor (connections.c). This fixes the original bug when the script is run in a terminal, but doesn't help for pipes, which will return false for isatty().An example R script and python script are provided to demonstrate the problem: R script (example.r): ===============f <- file("stdin") open(f) while(length(line <- readLines(f,n=1)) > 0) { write(line, stderr()) } Python3 script: ===========import sys, os, subprocess process = subprocess.Popen(['Rscript', 'example.r'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) for line in sys.stdin: process.stdin.write((line + '\n').encode('utf-8')) process.stdin.flush() Expected Behaviour: Run python script, each line entered is echoed back immediately by the R script - which is what happens on 3.4.4 Observed Behaviiour on >=3.5.0 (include devel): The R script does not process lines as they are sent, it only receives them when StdIn is closed. Best Regards Chris
Michael Lawrence
2018-Sep-13 12:14 UTC
[Rd] Bug 17432 in readLines with R >= 3.5.0 still a problem
Thanks, I responded to this on bugzilla. On Wed, Sep 12, 2018 at 9:04 AM Chris Culnane <christopher.culnane at unimelb.edu.au> wrote:> > Bug 17432 (https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17432) is still a problem when using pipes for IPC. > > The bug is evident when calling R from another process and trying to communicate via StdIn. R will buffer the input and not read lines until the buffer is exceeded or StdIn is closed by the sending process. This prevents interactive communication between a calling process and a child R process. > > From a quick look at the source code, it looks like the bug is caused by only disabling buffering when isatty() returns true for a file descriptor (connections.c). This fixes the original bug when the script is run in a terminal, but doesn't help for pipes, which will return false for isatty(). > > An example R script and python script are provided to demonstrate the problem: > > R script (example.r): > ===============> f <- file("stdin") > open(f) > while(length(line <- readLines(f,n=1)) > 0) { > write(line, stderr()) > } > > Python3 script: > ===========> import sys, os, subprocess > process = subprocess.Popen(['Rscript', 'example.r'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) > for line in sys.stdin: > process.stdin.write((line + '\n').encode('utf-8')) > process.stdin.flush() > > > Expected Behaviour: > Run python script, each line entered is echoed back immediately by the R script - which is what happens on 3.4.4 > > Observed Behaviiour on >=3.5.0 (include devel): > The R script does not process lines as they are sent, it only receives them when StdIn is closed. > > > Best Regards > > Chris > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >