PHP Implementation Multithreading _php Tutorial

Source: Internet
Author: User
PHP Implementation Multithreading
It is much easier for a server to send multiple requests to implement multiple processes. Can only be used in CLI mode. Can be used in special occasions, such as mail sending tasks.
Shared access to resources uses file locks and is not very reliable, primarily to be able to be used under WINDWOS, if it is necessary to consider switching to the corresponding semaphore mechanism (this extension can only be used for Xunix).

Instance
[PHP]
Define (' dir_php_exec ', ' PHP ');
Define (' Dir_main_exec ', __file__);
Define (' dir_tmp ', '/tmp ');
Require_once (' my_process.php ');

Class PP extends My_process_base {
Public function run ($param = null) {
for ($i = 0; $i < 4; $i + +) {
echo "111 $paramn";
Sleep (1);
}
}
}

Init_my_process ();
$obj = $GLOBALS [' Gal_obj_process_m '];
if ($obj->is_main ()) {
$obj->run_task (' pp ', ' a ');
$obj->run_task (' pp ', ' B ');
$obj->run_task (' pp ', ' C ');
$obj->run_task (' pp ', ' d ');
$obj->run_task (' pp ', ' B ');
$obj->set_max_run (10);
$obj->run ();
}

[/php]

Process Management class
[PHP]
/**
* @copyright movivi
* @author zhi
*
* $Id: getpage.php 2007-09-21 02:15:01z Fred $
*/
if (!defined (' dir_php_exec ')) define (' Dir_php_exec ', ' PHP ');
//if (!defined (' dir_main_exec ')) define (' dir_main_exec ', ');
if (!defined (' dir_tmp ')) define (' dir_tmp ', ');
/*****************************************************************************/
/* initialization */
define (' CMD_ Main_process_key ', ' Main_process_key ');
define (' Cmd_child_process_name ', ' child_process_name ');
define (' Cmd_child_process_param ', ' Child_process_param ');

function init_my_process () {
$GLOBALS [' gal_obj_cmd '] = new my_cmd_argv ();
$key = $GLOBALS [' Gal_obj_cmd ']->get_value (Cmd_main_process_key);
$key = $key = = = False? ": $key;
$GLOBALS [' gal_obj_process_m '] = new My_process_m ($key);
if (! $GLOBALS [' Gal_obj_process_m ']->is_main ()) $GLOBALS [' Gal_obj_process_m ']->run ();
}

/**
* PHP Multi-Process class
*
* You need to inherit from this object and then implement your own run processing
*/
Abstract class My_process_base {
Public function __construct ($auto _run=true, $name = ") {
}

Public Function __destruct () {
echo "@endn";
}

Abstract public Function run ($param = null);
}


Class MY_CMD_ARGV {
Private $cmd _argv = Array ();
Public Function __construct () {
$ARGV = $_server[' argv ');
for ($i = 1; $i < count ($argv); $i + +) {
$cmd = explode (' = ', $argv [$i]);
$this->cmd_argv[$cmd [0]] = isset ($cmd [1])? $cmd [1]: ";
}
}

Public Function Get_key ($key) {
return Isset ($this->cmd_argv[$key]);
}

Public Function Get_value ($key) {
return Isset ($this->cmd_argv[$key])? $this->cmd_argv[$key]: false;
}
}

/**
* PHP Multi-process Management class
* Multi-process processing can be implemented in PHP, only available in console mode
* The current signal implementation mechanism adopts file mode
*
*/
Class My_process_m {
/**
* @var Array $task _list
* Process List
*/
Private $task _list = Array ();
Private $lock _list = Array ();
Private $lock = null;
Private $is _main = false;
Private $max _run = 3600000;

Private Function release_lock ($key = null) {
$lock = & $this->lock_list;
if (!is_null ($key)) {
$key = MD5 ($this->build_lock_id ($key));
if (Isset ($lock [$key])) {
if (Is_resource ($lock [$key][0]) fclose ($lock [$key][0]);
Unlink ($lock [$key][1]);
Unset ($lock [$key]);
}
return true;
}

foreach ($lock as $k = = $h) {
if (Is_resource ($h)) fclose ($h);
Unset ($lock [$k]);
}
return true;
}

Private Function release_task ($key = null) {
$task = & $this->task_list;
if (!is_null ($key) && isset ($task [$key])) {
if (Is_resource ($task [$key])) pclose ($task [$key]);
Unset ($task [$key]);
} else {
foreach ($task as $k = = $h) {
if (Is_resource ($h)) pclose ($h);
Unset ($task [$k]);
}
}
return true;
}

Private Function build_lock_id ($key) {
Return dir_tmp. Directory_separator. $key. ' _sem.lock ';
}

protected function run_child_process () {
$class = $GLOBALS [' Gal_obj_cmd ']->get_value (cmd_child_process_name);
$param = $GLOBALS [' Gal_obj_cmd ']->get_value (Cmd_child_process_param);
$param = $param = = "? Null:unserialize (Base64_decode (Trim ($param)));
$obj = new $class ();
$obj->run ($param);
$this->task_list[] = $obj;
}

Public function __construct ($lock = ") {
if ($lock = = =) {
$this->is_main = true;
$key = MD5 (Uniqid ()). ' _main.my_process ';
$lock = Array ($key, $this->get ($key));
} else {
$this->is_main = false;
$lock = Array ($lock, 0);
}
$this->lock = $lock;
}

Public Function __destruct () {
$this->release_lock ();
$this->release_task ();
}

/**
* Stop All Processes
*
*/
Public Function Stop_all () {
}

/**
* Whether it is the main process
*
*/
Public Function Is_main () {
return $this->is_main;
}

/**
* Is there an active signal?
*
* @param string $key
* @return BOOL
*/
Public function exist ($key) {
Return file_exists ($this->build_lock_id ($key));
}

/**
* Get a signal
*
* @param string $key
* @param int $max _acquire Maximum number of requests blocked
* @return Mix if a signal ID is returned successfully
*
*/
Public function Get ($key, $max _acquire=5) {
$FN = $this->build_lock_id ($key);
if (Isset ($this->lock_list[md5 ($FN))) return false;
$id = fopen ($FN, ' A + ');
if ($id) $this->lock_list[md5 ($FN)] = Array ($id, $FN);
return $id;
}

/**
* Release a signal
*
* @param string $key
* @return BOOL If successful return a signal true
*
*/
Public function Remove ($key) {
return $this->release_lock ($key);
}

/**
* Get a signal
*
* @param string $id signal ID
* @param bool $block is blocked
*/
Public function Acquire ($id, $block =false) {
if ($block) {
Return Flock ($id, LOCK_EX);
} else {
Return Flock ($id, LOCK_EX + lock_nb);
}
}

/**
* Release a signal
*
*/
Public Function Release ($id) {
Flock ($id, lock_un);
}
Public Function Run_task ($process _name, $param =null) {
$this->task_list[] = Popen (dir_php_exec. '-f '. Dir_main_exec. ' -- '
. Cmd_child_process_name. '=' . $process _name. ' '
. Cmd_child_process_param. '="' . Base64_encode (Serialize ($param)). '" '
. Cmd_main_process_key. '="' . $this->lock[0]. '" ',
' R ');
}

Public function Run ($auto _run = True) {
if ($this->is_main) {
$ps = & $this->task_list;
$max _run = & $this->max_run;
$id = 0;
do {
echo "Process----------------------------------------: N";
$c = 0;
foreach ($ps as $k = = $h) {
$c + +;
$msg = Fread ($h, 8000);
if (substr ($msg, -5, 4) = = = ' @end ') {
echo "End process:[$k [$id] echo n{$msg} n";
$this->release_task ($k);
} else {
echo "process:[$k [$id] echo n{$msg} n";
}
}
Sleep (1);
} while ($auto _run && $id + < $max _run && $c > 0);
} else {
$this->run_child_process ();
}
}

Public Function Set_max_run ($max =1000) {
$this->max_run = $max;
}
}


http://www.bkjia.com/PHPjc/445002.html www.bkjia.com true http://www.bkjia.com/PHPjc/445002.html techarticle PHP Implementation Multi-threaded server to send multiple requests to achieve multiple processes to facilitate a lot. Can only be used in CLI mode. Can be used in special occasions, such as mail sending tasks. Resource sharing visit ...

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