PHP multi-thread instance (PHP multi-thread class)

Source: Internet
Author: User
Tags php multithreading

This article provides you with two PHP multithreading. One is to imitate java's multithreading, and the other is to use curl to implement multi-threaded sequential image collection. Let's take a look at it.

Use WEB servers to implement PHP multithreading.

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, not in the true sense.

But in any case, it can still meet some of our needs, and can still be used in the need of features similar to multithreading.

The Code is as follows: Copy code

 

/**
* @ Title: PHP multithreading class (Thread)
* @ Version: 1.0
* @ Author: phper.org.cn <web@phper.org.cn>
* @ Published: 2010-11-2
*
* 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 );
}
}
}
}
}


Usage:

The Code is as follows: Copy code

$ 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.

Example 2.Use curl to implement multi-threaded image download

It is actually a multi-threaded class implemented by php using curl.) with this class, we can also use this class to execute multi-threaded tasks!

The Code is as follows: Copy code

 

<? Php
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;
}
}

Example of multi-thread download for php: Download remote Images

The Code is as follows: Copy code


$ Curl_mul = new curl_multi ();
$ Curl_mul-> setUrlList (array ('HTTP: // www. bKjia. c0m/img/logo.jpg ', 'HTTP: // www. bKjia. c0m/img/logo.jpg ', 'HTTP: // www. bKjia. c0m/img/logo.jpg '));
$ A = $ curl_mul-> execute ();
$ I = 1;
Foreach ($ a as $ v ){
Using filename=polici.'.gif ';
$ Fp2 = @ fopen ($ filename, 'A ');
Fwrite ($ fp2, $ v );
Fclose ($ fp2 );
$ I ++;
}

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.