Implementing multi-threading in php_PHP tutorial
PHP implements multi-threading
It is much more convenient for the server to send multiple requests to implement multi-process. Can only be used in cli mode. Can be used in special occasions, such as email sending tasks, etc.
The shared access to resources uses file locks, which is not very reliable. It is mainly for use under Windows. If it is really necessary, you can consider using the corresponding semaphore mechanism yourself (this extension can only be used for xUNIX).
Example
[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++) {
$obj = $GLOBALS['gal_obj_process_m'];
if ($obj->is_main()) {
$obj->run_task('pp', 'a');
$obj->run_task('pp', 'b');
$obj->run_task('pp', 'd');
//$obj->run_task('pp', 'b');
$obj->set_max_run(10);
$obj->run() ;
}
[/php]
Process Management Class
[php]
/**
* @author 徐智
* $Id: getPage.php 11 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);
}
/**
* php multi-process class
*
* You need to inherit from this object and then implement your own run processing
*/
public function __construct($auto_run=true, $name='') {
}
public function __destruct () {
echo "@endn";
}
abstract public function run($param = null);
class my_cmd_argv {
public function __construct() {
$argv = $_SERVER['argv'];
]) ? $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 ; }
}
/**
* Multi-process processing can be implemented in PHP and can only be used in console mode
* The current signal implementation mechanism uses file mode
*
*/
/**
* @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 already 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 The maximum number of blocked requests
* @return mix If successful, return a signal ID
*
*/
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 Whether to block
*/
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;
}
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
