How to implement multithreading in php
This article mainly shows you "how to implement multithreading in php". The content is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "how to implement multithreading in php".
Public function testa () {
$id = $_ REQUEST ['id']
For ($iAdd ($data)
}
Public function testb () {
$id = $_ REQUEST ['id']
$time = time ()
$data ['time'] = $time
M ('test')-> add ($data)
}
Php Bensheng does not support multithreading. If you want the above two methods to execute at the same time, I use curl. Here is the code for curl parallel these two methods.
This section is hidden, 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-threaded CURL instance
$ch = curl_init ()
/ / set parameters related to CURL
Curl_setopt ($ch, CURLOPT_URL, $v)
Curl_setopt ($ch, CURLOPT_HEADER, 0)
Curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true)
Curl_setopt ($ch, CURLOPT_TIMEOUT, 30)
/ / load the process into the instance
Curl_multi_add_handle ($mh,$ch)
/ / add to the circular array
$handles [] = $ch
}
/ / execute CURL multithreaded instance
$running=null
Do
{
Curl_multi_exec ($mh,$running)
/ / interval 0.25s
/ / usleep (250000)
} while ($running > 0)
/ / obtain the collection content
For ($iSuppli