且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

IBrokers reqMktData,如何向回调函数添加超时?

更新时间:2023-12-04 19:44:04

您可以使用 R.utils .我没有进行测试,但是我敢肯定,将evalWithTimeout封装在while循环中会达到您想要的目的.

You can use evalWithTimeout from the R.utils. I didn't test it, but I'm pretty sure wrapping evalWithTimeout around the while loop would achieve what you're after.

library(R.utils)

evalWithTimeout(
   while (TRUE) {
       socketSelect(list(con), FALSE, NULL)
       curMsg <- .Internal(readBin(con, "character", 1L,
           NA_integer_, TRUE, FALSE))
       if (!is.null(timestamp)) {
           processMsg(curMsg, con, eWrapper, format(Sys.time(),
             timestamp), file, ...)
       }
       else {
           processMsg(curMsg, con, eWrapper, timestamp,
             file, ...)
       }
       if (!any(sapply(eWrapper$.Data$data, is.na)))
           return(do.call(rbind, lapply(eWrapper$.Data$data,
             as.data.frame)))
   }, timeout=5, onTimeout="warning")