PHP Multi-Threading Instance (PHP multithreaded Class) _php tutorial

Source: Internet
Author: User
Article to organize two PHP multithreading, one is to imitate the multi-threaded Java, the other is the use of curl to achieve multi-line program collection of pictures, let's look at the following together.

Through the Web server to implement the PHP multithreading function.

Of course, people who have a deep understanding of multithreading know that multithreading can only mimic the effects of multithreading through Web servers, not the real multi-threading.

However, it can still meet some of our needs, in the need for similar features such as multithreading can still use this class.

The code is as follows Copy Code

/**
* @title: PHP multithreaded Class (thread)
* @version: 1.0
* @author: phper.org.cn < web@phper.org.cn >
* @published: 2010-11-2
*
* PHP Multithreaded 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);
}
}
}
}
}


How to use:

The code is as follows Copy Code

$thread = new Thread ();

$thread->addthread (' func1 ', ' info1 ');

$thread->addthread (' Func2 ', ' Info2 ');

$thread->addthread (' func3 ', ' Info3 ');

$thread->runthread ();


Description

Addthread () is the addition of a thread function, the first argument is the function name, and the following argument (optional) is the argument passed to the specified function.

Runthread () is a function that executes a thread.

Example 2. Using curl to achieve multi-threaded download pictures

In fact, PHP using curl implementation of a multi-threaded class, with this class, we can also use this class to perform multi-threaded tasks!

The code is as follows Copy Code

Class curl_multi{
Private $url _list=array ();
Private $curl _setopt=array (
' Curlopt_returntransfer ' = 1,//results returned to the variable
' Curlopt_header ' = 0,//whether to return HTTP headers
' Curlopt_nobody ' = 0,//If you need to return content
' Curlopt_followlocation ' + 0,//automatic tracking
' Curlopt_timeout ' = 6//time-out (s)
);
function __construct ($seconds =30) {
Set_time_limit ($seconds);
}
/*
* Set 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;
}
/*
* 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 ', ' $ A ', $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 multi-threaded Download class example: Download remote picture

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) {
$filename = $i. '. gif ';
$FP 2= @fopen ($filename, ' a ');
Fwrite ($fp 2, $v);
Fclose ($fp 2);
$i + +;
}

http://www.bkjia.com/PHPjc/632713.html www.bkjia.com true http://www.bkjia.com/PHPjc/632713.html techarticle article to organize two PHP multithreading, one is to imitate the multi-threaded Java, the other is the use of curl to achieve multi-line program collection of pictures, let's look at the following together. Through W ...

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