toss for a week, and finally put the task to submit; The task is to implement scheduled tasks and automatic tasks under Linux, which can occur concurrently, so it is necessary to consider multithreading. I also found a lot of information on the Internet does not seem to meet my needs, and finally combined with the wisdom of predecessors and their own research, or achieved;
Public function Testa () {
$id = $_request[' id '];
For ($i =0; $i <1000000; $i + +) {
}
$time = time ();
$data [' time '] = $time;
M (' Test ')->add ($data);
}
Public function Testb () {
$id = $_request[' id '];
$time = time ();
$data [' time '] = $time;
M (' Test ')->add ($data);
}
PHP native is not support multi-threaded, if you want the above 2 methods to execute the same time, I used the curl, is the curl parallel these 2 methods of code
This section is set to hide, you have replied, the following is the hidden content
//Load a multi-process curl Instance
$mh = Curl_multi_init ();
$handles = Array ();
$array =array ("Localhost:1001/index.php/test/testa", "Localhost:1001/index.php/test/testb");
foreach ($array as $k = $v)
{
//Create a single thread curl instance
$ch = Curl_init ();
//Set Curl related parameters
curl_setopt ($ch, Curlopt_url, $v);
curl_setopt ($ch, Curlopt_header, 0);
curl_setopt ($ch, Curlopt_returntransfer, true);
curl_setopt ($ch, Curlopt_timeout,);
//load the process into an instance
Curl_multi_add_handle ($MH, $ch);
//join in loop array
$handles [] = $ch;
}
//Perform curl multithreaded instances
$running =null;
Do
{
curl_multi_exec ($MH, $running);
//Interval 0.25S
//usleep (250000);
} while ($running > 0);
//Get collected content
For ($i =0; $i <count ($handles); $i + +)
{
Curl_multi_remove_handle ($MH, $handles [$i]);
}
//Close instance
Curl_multi_close ($MH);
Developing source--php to realize multithreading