[R-pkg-devel] socketConnection, delay when reading from
Jeff Newmiller
jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Sun Nov 28 00:03:40 CET 2021
Google?
https://developer.r-project.org/Blog/public/2020/03/17/socket-connections-update/
https://www.digitalocean.com/community/tutorials/understanding-sockets
https://developer.ibm.com/tutorials/l-sockpit/
On November 27, 2021 2:36:48 PM PST, Ben Engbers <ben.engbers using gmail.com> wrote:
>Hi,
>
>Looks promising! Where can I find more information or tutorials about
>psock or parallel computing?
>
>Best,
>Ben
>
>Op 27-11-2021 om 20:19 schreef Tomas Kalibera:
>> On 11/27/21 8:05 PM, Tomas Kalibera wrote:
>>
>> This is an extended demo with socketSelect() used to wait on the client
>> for some data to be available, to avoid consuming too much CPU by
>> polling. To be pasted into two R sessions running on the same computer.
>> You would have to replace the function done() with something figuring
>> out from the data whether it is complete or not, based on the protocol.
>>
>> Best
>> Tomas
>>
>>
>> # the client
>>
>> con2 <- socketConnection("localhost", port = 6011, open = "rb")
>> cat("Connected...\n")
>> total <- 0
>>
>> done <- function(n) {
>> n >= 2e8
>> }
>>
>> while(!done(total)) {
>> cat("Waiting for data to become ready...\n")
>> socketSelect(list(con2))
>> cat("Reading data...\n")
>> r <- readBin(con2, "raw", 1024)
>> total <- total + length(r)
>> cat("Read", length(r), "bytes (total ", total, ").\n")
>> }
>> close(con2)
>>
>> # the server
>>
>> n <- 1e8
>> w <- as.raw(runif(n, 0, 255))
>> con1 <- socketConnection(port = 6011, blocking = TRUE, server = TRUE,
>> open="a+b")
>> cat("Connected...\n")
>> writeBin(w, con1)
>> cat("Sent data the first time, sleeping...\n")
>> Sys.sleep(10)
>> cat("Sending data the second time...\n")
>> writeBin(w, con1)
>> cat("Data sent to client...\n")
>> close(con1)
>>
>>>
>>> Best
>>> Tomas
>>>
>>> # the client
>>>
>>> con2 <- socketConnection("localhost", port = 6011, open = "rb")
>>> cat("Connected...\n")
>>> total <- 0
>>>
>>> done <- function(n) {
>>> n >= 1e8
>>> }
>>>
>>> while(!done(total)) {
>>> r <- readBin(con2, "raw", 1024)
>>> total <- total + length(r)
>>> cat("Read", length(r), "bytes (total ", total, ").\n")
>>> }
>>> close(con2)
>>>
>>> # the server
>>>
>>> n <- 1e8
>>> w <- as.raw(runif(n, 0, 255))
>>> con1 <- socketConnection(port = 6011, blocking = TRUE, server = TRUE,
>>> open="a+b")
>>> cat("Connected...\n")
>>> writeBin(w, con1)
>>> cat("Data sent to client...\n")
>>> close(con1)
>>>
>>>>
>>>> Ben
>>>>
>>>> ______________________________________________
>>>> R-package-devel using r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
--
Sent from my phone. Please excuse my brevity.
More information about the R-package-devel
mailing list