make PHP wait until a function completes?

54,963

Solution 1

PHP is single-threaded, which means that it executes one instruction at a time before moving on. In other words, PHP naturally waits for a function to finish executing before it continues with the next statement.

I tried executing your code on my machine, and it properly waited and waited until I modified the file before completing the function and displaying the message.

I can't even see anything in your code that might be failing. Since the creation of $lfilemod is performed in the same way as the check in the while loop, the condition of that loop would return TRUE and execute even if there was a problem with the file (filemtime would return FALSE on both errors, so the condition would read (FALSE == FALSE, which is obviously TRUE).

Does your PHP script modify the file at all before running that loop? If it does, then the initial value returned by filemtime might be the modification time from when the script originally started. When you run clearstatcache in the loop, you'll pick up the new modification time caused by your changes earlier in the script.

My advice:

  • Try running clearstatcache before setting the value of $lfilemod so that you know the value is clean, and that you're comparing apples-to-apples with what is being checked in the loop.

  • Make sure the file really isn't being modified. Try placing a couple debugging lines at the start and end of your code which prints out the last modification time for the file, then comparing the two yourself to see if PHP is reporting seeing a change in modification time.

  • This should go without saying, but you should make sure that PHP is configured to display all errors during development, so you are immediately shown when and how things go wrong. Make sure that the display_errors setting is turned On in your php.ini file (or use ini_set() if you can't modify the file itself), and that your error_reporting() is set to E_ALL | E_STRICT for PHP <= 5.3, or E_ALL for PHP 5.4 (E_STRICT is part of E_ALL as of that version). A better way is to just set your error reporting to -1 which effectively turns on all error reporting regardless of PHP version.

Try running your code again with these modifications. If you see that the file really is being modified, then you know that your code works. If the file isn't being modified, you should at least have an error that you can look up, or ask us here.

Solution 2

Try assigning the function call to a variable. I think it doesn't wait for the return otherwise.

Share:
54,963
markasoftware
Author by

markasoftware

Web dev and Linux shizzle. All my projects are side projects.

Updated on January 18, 2020

Comments

  • markasoftware
    markasoftware over 4 years

    Is there any way to make PHP wait until a function returns before continuing?

    This is my code:

    <?php
    
        set_time_limit(0);
    
        function waitforchange($nof) {
            $lfilemod=filemtime($nof);
            while(filemtime($nof) == $lfilemod) {
                clearstatcache();
                usleep(10000);
            }
        }
    
        waitforchange('./blahblah.txt')
        sleep(5);
        echo 'done';
    
    ?>
    

    It is supposed to wait until blahblah.txt changes, wait another five seconds after that, then print out "done", however, it prints out "done" after five seconds, regardless whether the file actually changed.

  • markasoftware
    markasoftware over 11 years
    can't be. That code I entered in there was not copied from my real code, it is just extremely similar. My real code has the semicolon.
  • Mikeon
    Mikeon over 11 years
    If the semicolon wasn't there in the actual script, the code wouldn't run at all. PHP would report a parse error, which is a fatal error that would abort the script before even began to run.
  • markasoftware
    markasoftware over 11 years
    Thank you! The file was modified after the script began but before waitforchange() was called, so I added clearstatcache() to the beginning of waitforchange and it works! My also the php.ini settings were set incorrectly. That wasn't the cause of the problem, but it may help in the future.
  • Mikeon
    Mikeon over 11 years
    @Markasoftware Happy to help!
  • Radmation
    Radmation over 6 years
    What about file writing? I am splitting up a large CSV file into several smaller versions but my code tries to open up the smaller ones before they are created. Does file writing not pertain?