-
Notifications
You must be signed in to change notification settings - Fork 23
使用Pidfile v0.3
Qing Zhao edited this page Mar 26, 2015
·
1 revision
编写Daemon程序时,我们可以使用一个PID文件标记进程是否已经被创建,防止进程被重复启动,PID文件同时记录了进程号,以便于向进程发送信号。QPM中的pidfile正是用来管理PID文件的模块。
例子:
#pid_main.php
<?php
require_once 'qpm/pidfile/Manager.php';
use qpm\pidfile\Manager;
$man = new Manager(__FILE__.'.pid');
$man->start();
while(true) sleep(10);
?>
首次执行 php pid_main.php,我们能看到生成了 pid_main.php.pid文件,进程持续运行。 再次执行 php pid_main.php,脚本报错,提示进程已经存在。 Fatal error: Uncaught exception 'qpm\pidfile\Exception' with message 'process exists, no need to start a new one' in .../qpm/pidfile/Manager.php:41
此外,qpm\pid\Manager 还有getProcess 方法。
#pid_check.php
<?php
$man = new Manager(__DIR__.'/pid_main.php.pid');
echo $man->getProcess()->getPid();
?>
如果pid文件不存在或为空,则会抛出异常。 注意:getProcess方法获取的对象,并不100%确保是原进程。后续版本会改进这里的检测。
文档首页 ##教程
- 安装和使用QPM
- 使用Process创建daemon程序
- 使用Supervisor创建健壮的多进程程序
- 使用Supervisor实现基于队列的并行任务处理程序
- 使用Pid 防止进程重复启动
- 使用日志,接入第三方日志
##参考
##旧版本
##Guides
- Getting Started
- Create a daemon with Qpm/Process
- Create Robust Multiprocess Programs with Supervisor
- Create a Queue-based Parallel Task Processing Program with Supervisor
##References