Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • "Sleep" alternative

    Hello-- I am wondering if anyone knows of alternatives to the sleep command. I am running some complicated code--loops through each US state, calls in multiple other .do and .ado files and takes 12+ hours to run. Depending on server traffic, it seems like Stata sometimes can't catch up with itself and when I'm trying to save out a file I get the ". . . read -only; cannot be modified or erased" error. Our fix has been to add sleep commands after save commands to give Stata a few extra seconds, but we were hoping for a more efficient fix, something that does not eat up as much time unnecessarily. For those who are familiar, something more like the trylock command in SAS. Any ideas?

  • #2
    This has come up before, and somebody (unfortunately, I cannot remember who, nor find the original post) came up with a solution that basically wraps the -capture-d -save- command in a loop with a short -sleep- in between. So something like this:

    Code:
    local try_again 1
    while `try_again' {
        capture save my_file, replace
        if c(rc) == 0 {
            local try_again 0
        }
        else if c(rc) == 608 { // FILE NOT AVAILABLE
        {
             sleep 50
        }
        else {
            display as error "Unexpected error saving file"
            exit c(rc)
        }
    }
    Notes: Not tested, and my require adaptation to your situation. I'm pretty sure 608 is the error code you get with the situation you are describing. But review your log from the previous tries where you got that error message, and replace 608 but the correct number if that's not right.

    You also might want to tune that 50 millisecond sleep to something that works better in your situation. It really depends on the size of the files involved, the speed of the network connections, etc..

    Added: One warning: if the file is actually unavailable for some reason other than the I/O having fallen behind (e.g. it really is a read-only file) then this will hang in an infinite loop. I don't think Stata has any way to actually discern why the OS refuses a given output command: all you can do is ask Stata to try again. So you might want to consider modifying that code to put in a counter and stop trying after some finite number of attempts (and exit with an error message in that case).
    Last edited by Clyde Schechter; 05 Aug 2020, 14:55.

    Comment


    • #3
      Dear Clyde!

      I also got r(608) while saving in long loop like Rebecca. Tested your solution, and works perfectly!

      Only "{" in line 8 is wrong and must be removed.

      Comment

      Working...
      X