我想对我正在调用的函数设置时间限制,这样如果它超时,它就会移动到脚本的下一部分(我正在运行 source("...")
)。该函数是使用 Rcpp 编写的,具有 checkUserInterrupt();
贯穿始终。我一直在用withTimeout
在 R.utils
带选项的包装 onTimeout = silent
,但超时仍会暂停脚本的执行。
然后我在 tryCatch
内运行了这个返回 NULL
在任何错误的情况下,但它仍然停止我的脚本。我已经包含了一个复制这个问题的简短函数。
#include <Rcpp.h>
#include <chrono>
#include <thread>
using namespace Rcpp;
void sleep_fn(int n) // sleeps for n seconds
{
std::chrono::seconds dura( n );
std::this_thread::sleep_for( dura );
}
// [[Rcpp::export]]
int sleep_for(int n) // sleeps for n seconds and then returns n
{
for (int i = 0; i < n; i++)
{
checkUserInterrupt();
sleep_fn(1);
}
return(n);
}
现在,为了比较,让我们运行它以及 R 命令 Sys.sleep()
.看看这是否正常工作,但对于 sleep_for
它仍然抛出一个异常来停止我的脚本。> tryCatch(withTimeout(Sys.sleep(10), timeout = 5), error = function(ex) return(NULL))
NULL
> tryCatch(withTimeout(sleep_for(10), timeout = 5), error = function(ex) return(NULL))
Error in .Call(<pointer: 0x108cc17b0>, n) : reached elapsed time limit
> tryCatch(withTimeout(sleep_for(10), timeout = 5, onTimeout = "silent"), error = function(ex) return(NULL))
Error in .Call(<pointer: 0x108cc17b0>, n) : reached elapsed time limit
为什么不是 tryCatch
做我认为应该做的事?有什么我可以做的才能让它发挥作用吗?(我使用的是 R 版本 4.0.3、Rcpp 版本 1.0.6、R.utils 版本 2.10.1)
编辑:并且在 MacOS 11.3.1 上
最佳答案
试试这个模板,看看它是否有效。我用过它,它默默地存在。
# Timeout is in seconds
# https://www.rdocumentation.org/packages/R.utils/versions/2.7.0/topics/withTimeout
tryCatch(
{
withTimeout(some_function(), timeout = 60)
}
,TimeoutException = function(ex) {
print("Timeout of 60 seconds reached. Function will not be completely run.")
}
)
关于r - 如何在不停止执行脚本的情况下使用 tryCatch withTimeout 来使 Rcpp 函数超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67636203/