Code to stop execution of another code

瘦欲@ 提交于 2019-12-07 06:36:39

问题


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.

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.

It will be like pressing STOP button on R console.

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.


回答1:


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)
}

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.




回答2:


This is not really a solution using a script to monitor another process, but on Linux you might use ulimit -t 300 before starting your analysis, e.g., using Rscript code.R.

Then the kernel will kill you job, when the cpu time exceeds 300 seconds. Note that this applies to all commands executed from the same console after the ulimit command.




回答3:


Another not really answer. Sometimes i have to make a loop through each row of my dataset or i have to merge i guess 10 000 datasets into one.

When i do it inside loop i hide

k <- k  + 1 
print(k)

so i can trace my code, and see how much i already done. Ofcoruse you can improve this and trace time between each i in loop with proc.time(



来源:https://stackoverflow.com/questions/36084656/code-to-stop-execution-of-another-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!