Home Database Redis How does php enable the Swoole/Pool process pool to implement Redis persistent connections?

How does php enable the Swoole/Pool process pool to implement Redis persistent connections?

May 27, 2023 pm 05:55 PM
php redis swoole

php Let Swoole | Pool process pool implement Redis persistent connection

This module implements the process pool based on the Manager process management of Swoole\Server. It can manage multiple work processes. Compared with Process to implement multiple processes, Process\Pool is simpler and has a higher encapsulation level. Developers can implement process management functions without writing too much code. With Co\Server, pure coroutine style can be created. , a server-side program that can utilize multi-core CPUs.

Swoole process pool implements redis data reading

In the following case, the Redis process pool is started through WorkerStart and the Redis list data is read persistently; when WorkerStop disconnects all connections Recycle all child processes.

Step one: Encoding code

File: d10.php

<?php

use Swoole\Process;
use Swoole\Coroutine;

// 指定5个工作进程
$pool = new Process\Pool(5);
// 设置启用协程
$pool->set([&#39;enable_coroutine&#39; => true]);

/**
* onWorkerStart 子进程启动
* @param \Swoole\Process\Pool $pool Pool对象
* @param int $workerId   WorkerId当前工作进程的编号,底层会对子进程进行标号
**/
$pool->on("WorkerStart", function (Process\Pool $pool, $workerId) {
// 输出当前工作进程
   echo "Worker #{$workerId} is started\n";

   // 实例化化连接redis
   $redis = new Redis();
   $redis->pconnect(&#39;127.0.0.1&#39;, 6379);
   // 指定redis键
   $key = "key1";

   // 循环读取列表数据
   while (true) {
    // 弹出列表最后一个元素
       $msgs = $redis->brpop($key, 2);
       // 元素值为空则跳过
       if ( $msgs == null) {
           continue;
       }
       // 打印获取的值
       var_dump($msgs);
       echo "Processed by Worker#{$workerId}\n";
   }
});

// 子进程结束
$pool->on("WorkerStop", function ($pool, $workerId) {
   echo "Worker#{$workerId} is stopped\n";
});

// 启动工作进程
$pool->start();
Copy after login

Step two: Start the Redis service and write list data through the client

This case requires PHP to install the redis extension

# 通过redis客户端连接
./redis-cli

127.0.0.1:6379> lpush key1 &#39;world&#39;
Copy after login

Copy code

Step 3: Run d10.php

php d10.php
Copy after login

Step 4: View the process

ps aux | grep php
root        938  0.0  1.2 129164 12412 ?        Ss   Apr21   0:00 php-fpm: master process (/usr/local/php-8.0.1/etc/php-fpm.conf)
www         951  0.0  0.6 129164  6636 ?        S    Apr21   0:00 php-fpm: pool www
www         952  0.0  0.6 129164  6640 ?        S    Apr21   0:00 php-fpm: pool www
root      12327  0.0  1.2 126992 12800 pts/2    S+   00:02   0:00 php d10.php
root      12328  0.0  0.7 131096  7444 pts/2    S+   00:02   0:00 php d10.php
root      12329  0.0  0.7 131096  7448 pts/2    S+   00:02   0:00 php d10.php
root      12330  0.0  0.7 131096  7448 pts/2    S+   00:02   0:00 php d10.php
root      12331  0.0  0.7 131096  7448 pts/2    S+   00:02   0:00 php d10.php
root      12332  0.0  0.7 131096  7448 pts/2    S+   00:02   0:00 php d10.php
root      12355  0.0  0.0 112812   976 pts/3    R+   00:09   0:00 grep --color=auto php
Copy after login

Step 5: Output the result

php d10.php

Worker #1 is started
Worker #2 is started
Worker #3 is started
Worker #4 is started
Worker #0 is started
array(2) {
 [0]=>
 string(4) "key1"
 [1]=>
 string(5) "world"
}
Processed by Worker#1
Copy after login

d10.php file will always be blocked after running and keep reading the redis list data. Once the Redis list is output, it will be immediately popped up and printed on the screen.

The above is the detailed content of How does php enable the Swoole/Pool process pool to implement Redis persistent connections?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

See all articles