3

I have a web application that allows the users to upload DBF files and the app will store contents into an SQL database. The row count range from a few thousands to about 80,000 rows and I have the following code

    if($file){

        $totalRows = dbase_numrecords($file);
        for($i = 1; $i <= $totalRows; $i++){

            $row = dbase_get_record_with_names($file, $i);
            //echo $row["BILL_NO"]." ";
            if(!empty(trim($row["STATUS"]))){ //save to database if column is not empty
                
                $data = [
                   //array data from the row
                ];

                $db->table("item_menu")->replace($data);

            }

            if($i%1000 == 0) //Sleep call here every 1000 rows
                sleep(1);

        }

        echo "done";

    }

This function, once done will be called once per day and ideally just called/run in the background. However, when I do not place the sleep function, the server doesn't serve any pages until the loop completes, which can take from a few seconds to about a minute of unresponsiveness, but when the sleep function is added, the server continuously serve pages to different users.

My question is, does the sleep function help free up the current thread and process other requests during the sleep period?

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Nathan
  • 163
  • 10
  • 1
    It delays the script. Rather than freeing up the execution. Most likely fush is used somewhere before you are sleeping, hence it flushes the output before you execute sleep. – Dhaval Chheda May 12 '21 at 06:24

1 Answers1

1

If you use sleep() function then you will end up executing all the thing in a single thread causing a pause on the whole process. You should go for php v8.1 for that kind of process handling.

Sohan Arafat
  • 93
  • 2
  • 16