[R-pkg-devel] socketConnection, delay when reading from
Ben.Engbers
Ben@Engber@ @end|ng |rom be-|og|c@|@n|
Sat Nov 27 12:50:25 CET 2021
That's what I figured out also😉. The problem is that even when I reduce the size to 2 bytes and there is only 1byte available in the socket, the code will wait for 1 second which is the minimum time-out😔.BenVerzonden vanaf mijn Galaxy
-------- Oorspronkelijk bericht --------Van: Duncan Murdoch <murdoch.duncan using gmail.com> Datum: 27-11-2021 12:02 (GMT+01:00) Aan: Ben.Engbers using Be-Logical.nl, r-package-devel using r-project.org Onderwerp: Re: [R-pkg-devel] socketConnection, delay when reading from On 27/11/2021 4:09 a.m., Ben Engbers wrote:> > Hi,> > I have been working on a R-client for the BaseX XML-database and version> 0.9.2 is nearly finished (submitting version 0.9.0 was rejected by CRAN).> Version 0.3 of RBaseX can be found here> (https://cran.microsoft.com/web/packages/RBaseX/index.html).> > The client-server protocol specifies that the communication between the> client and the database is based on a socket. The code (below) shows how> I create that socket.> > Writing to the socket works perfect. Reading from the sockets (see> second codeblock) also produces correct results. The problem however is> that the timeout, as specified when initializing the socket, causes a 1> second delay for every read-operation.> I have experimented a lot with different settings and have been> searching a lot on internet, but I can't find any method to get rid of> that delay. (In C or C++ that should be easier but I have never before> had any need to use those languages).> The very first version of my client used a block-size of 1 when reading.> That gave acceptable response times for small query-results but reading> large responses from the database took very long time.> > Do you have any suggestions on how to cope with this problem?You are attempting to read 1024 bytes. If the socket returns fewer, the code presumably assumes it should wait for more. You should specify the number to read to be no larger than you'll receive. That's why reading a byte at a time works well for small reads.To handle larger ones, you need to know what size blocks you are going to receive with each read. If that varies unpredictably, I doubt if there's much you can do other than reducing the timeout.Duncan Murdoch> > Ben Engbers> > -----------------------------> CreateSocket = function(host, port = 1984L, username, password) {> tryCatch(> {conn <- private$conn <- socketConnection(> host = "localhost", port,> open = "w+b", server = FALSE, blocking = TRUE, encoding => "UTF-8", timeout = 1)> }, error = function(e) {> stop("Cannot open the connection")> }> )> > -----------------------------> > readBin_ <- function(conn) {> chars_read <- raw(0)> rd <- readBin(conn, what = "raw", 1024)> while(length(rd) == 1024) {> chars_read <- c(chars_read, rd)> rd <- readBin(conn, "raw", 1024)> }> if (length(rd) > 0) chars_read <- c(chars_read, rd)> return(chars_read)> }> > ______________________________________________> R-package-devel using r-project.org mailing list> https://stat.ethz.ch/mailman/listinfo/r-package-devel>
[[alternative HTML version deleted]]
More information about the R-package-devel
mailing list