Developing source--php to realize multithreading

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.