Search code examples
phpmultithreadingsimple-html-dom

execute three function simultaneously


I have sript php with three function like this:

public function a($html,$text)
{
    //blaa
return array();
}
public function b($html,$text){
    //blaa
return array();
}
public function c($html,$text){
    //blaa
return array();
}
require_once 'simple_html_dom.php';
$a=array();
$html=new simple_html_dom();
$a=$this->a($html,$text);
$b=$this->b($html,$text);
$c=$this->c($html,$text);
$html->clear();
unset($html);
$a=array_merge($a, $c);
$a=array_merge($a, $b);


  • a($html,$text) takes 5 seconds before giving a result

  • b($html,$text) takes 10 seconds before giving a result

  • c($html,$text) takes 12 seconds before giving a result

Thus the system takes 27 seconds before geving me a result, but I want take my result in 12 seconds. I can't use threads because my hosting does not support threads. How can I solve this problem?


Solution

  • If the wait time is caused by blocking IO (waiting for server response) then curl_multi might help.

    From the code you posted, though, it doesn't look like is your problem.

    It looks more like simple html dom is taking a long time to parse your html. That's not too surprising because it's not a very good library. If this is the case you should consider switching to DomXPath.