0

I'm trying to create a script that creates unique codes and writes them to a textfile. I've managed to generate the codes, and write them to the file.

Now my problem is the fact that my loop keeps running, resulting in over 92 000 codes being written to the file, before the server times-out.

I've done some logging, and it seems that everything works fine, it's just that after a certain amount of seconds, all my variables are reset and everything starts from scratch. The time interval after which this happens varies from time to time.

I've already set ini_set('memory_limit', '200M'); ini_set('max_execution_time',0); at the top of my script. Maybe there's a php time-out setting I'm missing?

The script is a function in a controller. I set the ini_set at the beginning of this function. This is the loop I'm going through:

public function generateAction() { 
    ini_set('memory_limit', '200M');
    ini_set('max_execution_time',0);

    $codeArray = array();
    $numberOfCodes = 78000;
    $codeLength = 8;

    $totaalAantal = 0;

    $file = fopen("codes.txt","a+");

    while(count($codeArray)<$numberOfCodes){
        $code = self::newCode($codeLength);
        if(!in_array($code,$codeArray))
        {
            $totaalAantal++;
            $codeArray[] = $code;
            fwrite($file,'total: '.$totaalAantal."\r\n");
        }
    }
    fclose($file);
}

In the file this would give something like this:

total: 1
total: 2
total: ...
total: 41999
total: 42000
total: 1
total: 2
total: ...
total: 41999
total: 42000

Thanks.

Edit: so far we've established that the generateAction() is called 2 or 3 times, before the end of the script, when it should only be called once.

9
  • can you post more code please. On how you set this up, was it in an actual loop? Or are you just giving the script a time limit. Commented Apr 27, 2012 at 13:19
  • To disable the time limit use set_time_limit(0); Commented Apr 27, 2012 at 13:38
  • @ThiefMaster That didn't make any difference. Commented Apr 27, 2012 at 13:43
  • @SpencerMay Is this enough information for you? Commented Apr 27, 2012 at 13:44
  • 1
    Have you considered running this script from the command line instead of via a web browser? Commented Apr 27, 2012 at 13:57

2 Answers 2

1

I already found the solution for this problem. The host's script limit was set to 90 seconds, and because this script had to run for longer, I had to run it via the command line.

Sign up to request clarification or add additional context in comments.

Comments

0

Taking account of the test with uniqid(), we can say that variables are not reseted, but the method generateAction() is called several times.

Since you code is probably synchronous, we may say that generateAction() is called several times because the main script is called several times.

What happens in detail?

Because of the nature of your algorithm, each pass in the loop is slower then the previous one. So the duration of executing generateAction() may be quite long.

You probably don't wait for the end, and you stop the process or even start the process from a new page. Nevertheless, the process don't really stop so soon, and it keeps running in back-end. I've observed such a behavior on my local WAMP/LAMP installation: the script is not actually stopped even if I stop the page, if I close the page, even if I close the navigator or if I restart Apache.

So it happens to you that several script processes are writing simultaneously in the codes.txt file.

In order to avoid this, you can for example lock the file during the loop using function flock().

3 Comments

You're absolutely right ! It does get called more than once. I added fwrite($file,'generateAction called !'."\r\n"); on the first line of generateAction() and it does get written to the file more than once. But locking it didn't help. What can cause this? It's a controller that only gets called when I open the page, so is the page auto-refreshing because of some PHP setting?
It may have many explanation. Is your code synchronous, or have you got some code triggered on a timer for example? It can also be some reluctant process that does not stop when you think it is. It can also be some Javascript that reload the page, or load some Ajax calls in back-end. You can just avoid Javascript in order to check this possibility. It can also be a HTTP refresh. This can be checks in the HTTP headers (with the Firefox add-on "Live HTTP Headers", for example).
I already found the solution for this problem. The host's script limit was set to 90 seconds, and because this script had to run for longer, I had to run it via the command line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.