Examples of PHP multithreading and usage
This example describes the PHP multithreading class and its usage. Share it with you for your reference. The specific analysis is as follows:
Generally, PHP multithreading can be implemented through WEB servers. Of course, anyone who has a deep understanding of multithreading knows that multithreading implemented through WEB servers can only imitate some of the effects of multithreading, it is not a real multi-thread.
But in any case, it can still meet some of our needs. We can still use this class in terms of features similar to multithreading. The Code is as follows:
Copy codeThe Code is as follows :/**
* @ Title: PHP multithreading class (Thread)
* @ Version: 1.0
*
* PHP multithreading application example:
* 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 );
}
}
}
}
}
The Code is as follows:
Copy codeThe Code is as follows: $ thread = new thread ();
$ Thread-> addthread ('function1', 'info1 ');
$ Thread-> addthread ('func2', 'info2 ');
$ Thread-> addthread ('func3', 'info3 ');
$ Thread-> runthread ();
Note:
Addthread () is used to add a thread function. The first parameter is the function name, and the subsequent parameter (optional) is the parameter passed to the specified function.
Runthread () Is the function of the execution thread.
PHP instance: Using curl for multi-thread image download
In fact, php uses curl to implement a multi-threaded class. With this class, we can also use this class to execute multi-threaded tasks. The Code is as follows:
Copy codeThe Code is as follows: class curl_multi {
Private $ url_list = array ();
Private $ curl_setopt = array (
'Curlopt _ RETURNTRANSFER '=> 1, // The result is returned to the variable.
'Curlopt _ header' => 0, // whether the http header needs to be returned
'Curlopt _ nobody' => 0, // whether to return the content
'Curlopt _ followlocation' => 0, // auto-tracking
'Curlopt _ timeout' => 6 // TIMEOUT (s)
);
Function _ construct ($ seconds = 30 ){
Set_time_limit ($ seconds );
}
/*
* Set the URL
* @ 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;
}
/*
* Execution
* @ 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',' $ 0', $ 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]); // release resources
}
Curl_multi_close ($ mh );
Return $ res;
}
}
Php uses a multi-thread download example to download remote images. The Code is as follows:
Copy codeThe Code is as follows: $ curl_mul = new curl_multi ();
$ Curl_mul-> setUrlList (array ('HTTP: // response '));
$ A = $ curl_mul-> execute ();
$ I = 1;
Foreach ($ a as $ v ){
Using filename=polici.'.gif ';
$ Fp2 = @ fopen ($ filename, 'A ');
Fwrite ($ fp2, $ v );
Fclose ($ fp2 );
$ I ++;
}
I hope this article will help you with PHP programming.