停止执行另一个代码的代码 [英] Code to stop execution of another code

查看:112
本文介绍了停止执行另一个代码的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个R代码,该代码进行了一些数据分析并返回了TRUE/FALSE. 有时,输入数据太大,代码就一直在运行.

I have a R code which does some data analysis and returns TRUE/FALSE. Sometimes, the input data is too large and the code just keeps running.

我想要一个脚本来监视我的数据分析代码,并且如果它不返回任何内容(例如600 seconds),那么它将暂停正在运行的代码并执行其他操作.

I want a script that will monitor my data analysis code and if it doesn't return anything, in say 600 seconds, then it halts the running code and do something else.

就像按R console上的STOP按钮一样.

我了解stopbreakexit等.但是这些将不会有用,因为由于代码仍在运行其数据分析循环,因此代码无法到达这些语句.

I know about stop, break, exit, etc. But these won't be useful because code won't reach to these statements as it is still running its data analysis loop.

推荐答案

您可以使用setTimeLimit()并让您的主脚本调用辅助脚本,该辅助脚本将在开始时具有此功能.

You can use setTimeLimit() and have your main script call a secondary script which will have this function at the start.

setTimeLimit(elapsed = 10)
for(i in c(1:100)){
    cat(i,"\n")
    Sys.sleep(1)
}

这是您期望它如何工作的一个示例. 10秒钟后,我的工作将达到时间限制,尽管循环希望计数到100,但每秒增加一次.

this is an example of how you would expect it to work. After 10 seconds my job will reach the time limit, despite the loop wanting to count to 100, increasing an increment every second.

这篇关于停止执行另一个代码的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆