您的位置 首页 php

Swoole-2.1.2 进程池模块的使用

15235840122443f809c4d4a

Swoole-2.1.2 版本中我们将 Server 的进程管理模块封装成了 PHP 类,现在可以在 PHP 代码中使用 Swoole 的进程管理器了。

在实际项目中经常需要写一些长期运行的脚本,如基于 redis kafka rabbitmq 实现的多进程队列消费者,多进程爬虫等等。程序员需要使用 pcntl posix 相关的扩展库实现多进程编程,需要开发者具备深厚的 Linux 系统编程功底,否则很容易出现问题。

Swoole 提供的进程管理器来自于 Swoole\Server ,经过大量生产项目验证,稳定性和健壮性都非常高。可大大简化多进程脚本编程工作。

一、 创建进程池

PHP 代码中使用 new Swoole\Process\Pool 即可创建一个进程池, 构造方法 的第一个参数传入工作进程的数量。使用 on 方法设置 WorkerStart 即可在工作进程启动时执行指定的代码,可以在这里进行 while(true) 循环从 redis 队列中获取任务并处理。使用 start 方法启动所有进程,管理器开始进入 wait 状态。

$workerNum = 10;$pool = new Swoole\Process\Pool($workerNum);$pool->on("WorkerStart",  function  ($pool, $workerId) { echo "Worker#{$workerId} is started\n"; $redis = new Redis(); $redis->pconnect('127.0.0.1', 6379); $key = "key1"; while (true) { $msgs = $redis->brpop($key, 2); if ( $msgs == null) continue; var_dump($msgs); }});$pool->on("WorkerStop", function ($pool, $workerId) { echo "Worker#{$workerId} is stopped\n";});$pool->start(); 

使用进程管理器,可以保证工作进程的稳定性。

  • 某个工作进程遇到致命错误、主动退出时管理器会进行回收,避免出现僵尸进程

  • 工作进程退出后,管理器会自动拉起、创建一个新的工作进程

二、信号处理

Swoole 进程管理器自带了信号处理,向管理器进程发送:

  • SIGTERM 信号:中止服务,向所有工作进程发送 SIGTERM 关闭进程

  • SIGUSR1 信号:重启工作进程,管理器会逐个重启工作进程

在工作进程中可以配合使用 pcntl_signal pcntl_signal_dispatch 实现信号处理。

$pool->on("WorkerStart", function ($pool, $workerId) { $running = true; pcntl_signal(SIGTERM, function () use (&$running) { $running = false; }); echo "Worker#{$workerId} is started\n"; $redis = new Redis(); $redis->pconnect('127.0.0.1', 6379); $key = "key1"; while ($running) { $msgs = $redis->brpop($key, 2); pcntl_signal_dispatch(); if ( $msgs == null) continue; var_dump($msgs); }}); 

三、任务投递

Swoole 进程管理器自带了消息队列和 TCP-Socket 消息投递的支持。可设置监听系统队列或者 TCP 端口,接收任务数据。此项功能是可选的,要使用任务投递功能,需要对进程池对象设置 onMessage 回调。

消息队列

$pool = new Swoole\Process\Pool(2, SWOOLE_IPC_ msg QUEUE, 0x7000001);$pool->on("WorkerStart", function ($pool, $workerId) { echo "Worker#{$workerId} is started\n";});$pool->on("Message", function ($pool, $message) { echo "Message: {$message}\n";});$pool->start(); 

需要在构造方法的第二个参数传入SWOOLE_IPC_MSGQUEUE,第三个参数设置监听的消息队列KEY。其他程序中使用消息队列相关API就可以向工作进程投递任务了。

TCP 端口

$pool = new Swoole\Process\Pool(2, SWOOLE_IPC_SOCKET);$pool->on("WorkerStart", function ($pool, $workerId) { echo "Worker#{$workerId} is started\n";});$pool->on("Message", function ($pool, $message) { echo "Message: {$message}\n";});$pool->listen('127.0.0.1', 8089);$pool->start(); 

使用 TCP端口 监听,需要设置构造方法的第二个参数为SWOOLE_IPC_SOCKET,并使用listen方法设置监听的主机和端口。

底层使用了4字节长度+包体的协议。其他程序中向此端口发送数据时,需要在数据包之前增加一个长度字段。

$ fp  = stream_socket_client("tcp://127.0.0.1:8089", $errno, $errstr) or die("error: $errstr\n");$msg = json_encode(['data' => 'hello', 'uid' => 1991]);fwrite($fp, pack('N', strlen($msg)).$msg);fclose($fp); 

文章来源:智云一二三科技

文章标题:Swoole-2.1.2 进程池模块的使用

文章地址:https://www.zhihuclub.com/34636.shtml

关于作者: 智云科技

热门文章

评论已关闭

31条评论

  1. Oxaloacetate may also be formulated in rectal compositions such as suppositories or retention enemas, e

  2. 5 of sensory neuron soma Fig 2B; Table 2 and their corresponding axons Fig 2C were found to express eGFP

  3. Testicular biopsy may be indicated in some cases to exclude spermatogenic failure O Regan, MD, of Northwestern University Robert H

  4. Have spent the past couple of days celebrating our 3rd Wedding Anniversary in Chester, where we got married

  5. Understanding of the autophagy machinery offers therapeutic strategies for treating inflammatory diseases Better comparison of study results will then be possible

  6. A comparative analyses, some of erectile dysfunction If you have fluid retention, you may need to eat less sodium since salt encourages the body to retain water

  7. This is especially true with DHEA, a natural hormone reported to help with the fight against aging and a number of other conditions

  8. Importantly, the organoids derived from human CRC samples, especially tissues that represent the adenoma carcinoma transition stage, can grow without the niche factors WNT, R spondin, EGF, and Noggin; this is because, the mutational background of these samples ensure that the associated downstream signaling pathways are constitutively activated Drost et al

  9. Food here that are available and affordable 57 5 10 6 freshly isolated DPPIV hepatocytes were washed 2 times with Block s medium at 100 g in a 50 ml tube and the resuspended cells were mixed with 500 VP cell lentiviruses in 500 Ојl Block s medium containing 5 FBS, 25 ng ml EGF and 2 ITS

  10. When considering how best to use aromatase inhibitors in the treatment of postmenopausal breast cancer, researchers have considered several possibilities

  11. My girl have been waking up in the middle of the night and feeling nauseous I had some stomach issues the first time I did just Herceptin

  12. 21 Due to the short follow up time and lack of consistency in the available data, 22 biologic subtype is not part of IBTR Among the most common of these are

  13. Questor thinks that future is getting closer all the time as the economy and retail spending in the UK recover

  14. In some embodiments, the survival of the subject having cancer is inversely correlated to the number of the SL paired genes which are co inactive in the subject s tumor based on the determined SL network and the genomic profile of the subject s tumor

  15. I have a new Tamoxifen side effect to add to my long list T rage 02 breast cancers were associated with lower serum 25 OH D

网站地图