PHP controls the time of the loop operation

Execute a program in a loop, but during the loop execution process, it may time out and cause the program to die. Therefore, the maximum time of each loop operation needs to be limited. If it times out, it will directly disconnect the improvement process and continue to the next level of loop operation. Ctrip, multi-threading can complete this operation, but when you do not understand these advanced technologies, you can use this simple method instead.

TaskAsync.php

namespace TaskAsync;use Workerman\MySQL\Connection;class TaskAsync { 
    /** 
     * Asynchronous task 
     * @params $func The main function 
     to be executed asynchronously* @params $func The function to be executed asynchronously after timeout 
     * @params $maxTime Asynchronous execution timeout Unit of time: seconds s 
     * @params $params The parameters to be passed to $func 
     */ 
    public static function asyncTask(callable $func, $params = array(), $maxTime = 0, callable $func2 = null, $params2 = array()){ 
        pcntl_signal(SIGCHLD, SIG_IGN); //Install monitoring signal 
        $pid = pcntl_fork(); //Generate a thread 
        if ($pid == -1) { 
            exit();//Failure to create child process 
        } else if ($pid == 0) { 
            //Logical 
            try {  
                //Execute user function
                call_user_func_array($func, $params);
            } finally { 
                //Kill the process after execution 
                posix_kill(posix_getpid(), SIGKILL); 
                exit(0);//End the operation of the child process 
            } 
        } else if ($pid> 0) { 
            $t = time(); 
            while (true) { 
                $nPid = pcntl_wait($s, WNOHANG); 
                if ($nPid> 0) { 
                    break; 
                } else if ($nPid <0) { 
                    break; 
                } else if ($maxTime && time()-$ t> $maxTime) { 
                    //The default timeout time is 0, that is, the timeout time is not limited   
                    posix_kill($pid, SIGKILL);
                    if (!empty($func2)) { 
                        call_user_func_array($func2, $params2);
                    }
                    break;
                } else {
                    sleep(1);//每秒轮询检查
                }
            }
        }
    }

    public static function getMysqlConn() {
        $dbConfig = require(APP_PATH . '/database.php');
        return new Connection($dbConfig['hostname'], $dbConfig['hostport'], $dbConfig['username'], $dbConfig['password'], $dbConfig['database']);
    }}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152

index.php

use TaskAsync\TaskAsync;//Use while(true) { 
	$db = TaskAsync::getMysqlConn(); 
	//Database operation 
	$db->closeConnection(); 
	$data = [] ;//Incoming data 
	TaskAsync:: asyncTask(array(new Download(),'downLoadExcel'), 
                        array($data), 
                        60 * 60 * 10, 
                        function($data){ 
                            echo'execution timeout'; 
                        }, 
                        array($data) 
                    );}12345678910111213141516

Before pcntl_fork, there can be no database connection operation. Therefore, if the operation of the database is involved, you must reconnect to the database every time you loop. After performing the operation, remember to disconnect the database connection, otherwise it will prompt MySQL server has gone away!

Guess you like

Origin blog.csdn.net/zy17822307856/article/details/112798293