Examples of PHP multithreading and usage

Source: Internet
Author: User
Tags php multithreading
This article mainly introduces the PHP multithreading class and its usage. The example analyzes the implementation methods and application skills of the multithreading class, and deeply analyzes the example of downloading remote images, for more information, see

This article mainly introduces the PHP multithreading class and its usage. The example analyzes the implementation methods and application skills of the multithreading class, and deeply analyzes the example of downloading remote images, for more information, see

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:

The 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:

The 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:

The 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:

The Code is as follows:

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.