This article describes the PHP multithreaded classes and usage. Share to everyone for your reference. The specific analysis is as follows:
In general, through the Web server to achieve PHP multithreaded functions, of course, the multithreading has a deep understanding of people know through the Web server to achieve multithreading can only imitate some of the effects of multithreading, not the real meaning of multithreading.
However, it can still meet some of our needs, in the need for similar multithreaded functions can still use this class, the code is as follows:
Copy Code code as follows:
/**
* @title: PHP multithreaded Class (thread)
* @version: 1.0
*
* Examples of PHP multithreaded applications:
* require_once ' thread.class.php ';
* $thread = new Thread ();
* $thread->addthread (' Action_log ', ' a ');
* $thread->addthread (' Action_log ', ' B ');
* $thread->addthread (' Action_log ', ' C ');
* $thread->runthread ();
*
* Function Action_log ($info) {
* $log = ' log/'. Microtime (). '. log ';
* $txt = $info. "Rnrn". ' Set in '. Date (' H:i:s ', Time ()). (double) microtime (). "RN";
* $fp = fopen ($log, ' w ');
* Fwrite ($FP, $txt);
* Fclose ($FP);
* }
*/
Class Thread {
var $hooks = array ();
var $args = array ();
function Thread () {
}
function Addthread ($func)
{
$args = Array_slice (Func_get_args (), 1);
$this->hooks[] = $func;
$this->args[] = $args;
return true;
}
function Runthread ()
{
if (Isset ($_get[' flag '))
{
$flag = intval ($_get[' flag '));
}
if ($flag | | $flag = = 0)
{
Call_user_func_array ($this->hooks[$flag], $this->args[$flag]);
}
Else
{
for ($i = 0, $size = count ($this->hooks); $i < $size; $i + +)
{
$fp =fsockopen ($_server[' http_host '],$_server[' Server_port '));
if ($FP)
{
$out = "Get {$_server[' php_self ']}?flag= $i http/1.1rn";
$out. = "Host: {$_server[' http_host ']}rn";
$out. = "Connection:closernrn";
Fputs ($fp, $out);
Fclose ($FP);
}
}
}
}
}
Using the method, the code is as follows:
Copy Code code as follows:
$thread = new Thread ();
$thread->addthread (' func1 ', ' info1 ');
$thread->addthread (' Func2 ', ' Info2 ');
$thread->addthread (' func3 ', ' Info3 ');
$thread->runthread ();
Description
Addthread () is the add thread function, the first parameter is the function name, and then the argument (optional) is the argument passed to the specified function.
Runthread () is the function that executes the thread.
PHP instance: Using Curl to implement multithreading download pictures
PHP is actually using curl implementation of a multithreaded class, with this class, we can also use this class to perform multithreaded tasks, the code is as follows:
Copy Code code as follows:
Class curl_multi{
Private $url _list=array ();
Private $curl _setopt=array (
' Curlopt_returntransfer ' => 1,//result returned to the variable
' Curlopt_header ' => 0,//whether the HTTP headers need to be returned
' Curlopt_nobody ' => 0,//need to return the content
' Curlopt_followlocation ' => 0,//automatic tracking
' Curlopt_timeout ' => 6//timeout (s)
);
function __construct ($seconds =30) {
Set_time_limit ($seconds);
}
/*
* Set up Web site
* @list Array
*/
Public Function Seturllist ($list =array ()) {
$this->url_list= $list;
}
/*
* Set parameters
* @cutPot Array
*/
Public Function setopt ($cutPot) {
$this->curl_setopt= $cutPot + $this->curl_setopt;
}
/*
* Implementation
* @return Array
*/
Public Function execute () {
$MH =curl_multi_init ();
foreach ($this->url_list as $i => $url) {
$conn [$i]=curl_init ($url);
foreach ($this->curl_setopt as $key => $val) {
curl_setopt ($conn [$i],preg_replace ('/(Curlopt_w{1,})/ie ', ' $ ', $key), $val);
}
Curl_multi_add_handle ($MH, $conn [$i]);
}
$active =false;
do{
$MRC =curl_multi_exec ($MH, $active);
}while ($MRC = = Curlm_call_multi_perform);
while ($active and $MRC = = CURLM_OK) {
if (Curl_multi_select ($MH)!=-1) {
do{
$MRC =curl_multi_exec ($MH, $active);
}while ($MRC = = Curlm_call_multi_perform);
}
}
$res =array ();
foreach ($this->url_list as $i => $url) {
$res [$i]=curl_multi_getcontent ($conn [$i]);
Curl_close ($conn [$i]);
Curl_multi_remove_handle ($MH, $conn [$i]);/Free Resources
}
Curl_multi_close ($MH);
return $res;
}
}
PHP uses the multithreading download class example, downloads the remote picture, the code is as follows:
Copy Code code as follows:
$curl _mul=new Curl_multi ();
$curl _mul->seturllist (' http://www.jb51.net/img/logo.jpg ', ' http://www.jb51.net/img/logo.jpg ', ' http:// Www.jb51.net/img/logo.jpg '));
$a = $curl _mul->execute ();
$i = 1;
foreach ($a as $v) {
$filename = $i. gif ';
$FP 2= @fopen ($filename, ' a ');
Fwrite ($fp 2, $v);
Fclose ($fp 2);
$i + +;
}
I hope this article will help you with your PHP program design.