首页 > 解决方案 > R socketConnection - 读取和超时

问题描述

我(仍在)为 Basex 开发 R 客户端,这是一个强大的 XML 数据库。客户端使用 socketConnection: conn <- private$conn <- socketConnection(host = "localhost", port, open = "w+b", server = FALSE, blocking = TRUE, encoding = "UTF-8", timeout = 1)。应用程序使用此函数从连接中读取:

readBin_ <- function(conn, n) {
  chars_read <- raw(0)
  rd <- readBin(conn, what = "raw", BUF_SIZE)
  while(length(rd) == BUF_SIZE) {
    chars_read <- c(chars_read, rd)
    rd <- readBin(conn, "raw", BUF_SIZE)
    }
  if (length(rd) > 0) chars_read <- c(chars_read, rd)
  return(chars_read)
}

这是包测试的输出:

==> devtools::test(

ℹ Loading RBaseX
ℹ Testing RBaseX
✓ |  OK F W S | Context
✓ |   3       | 01_first [13.5 s]                     
✓ |   4       | 02_RbaseX [4.1 s]                     
✓ |   8       | 03_QueryClass [7.1 s]                 
✓ |  10       | 04_Utility [18.3 s]                   
✓ |  25       | 05_Wrappers [63.6 s]                  

══ Results ═══════════════════════════════════════════
Duration: 106.7 s

[ FAIL 0 | WARN 0 | SKIP 0 | PASS 50 ]

从这个输出中,我可以看出 01_first 包含 13 个测试。从数据库读取需要 13 秒,处理需要 0.5 秒。

问题1:
有没有办法从套接字读取并忽略超时?

问题 2:
R 套接字连接是否有任何 C++ 替代方案(以及如何使用它们的示例,我使用 C++ 的经验很少)

标签: rsockets

解决方案


推荐阅读