sweelix / daemon
此软件包已被 废弃 且不再维护。未建议替代软件包。
PHP 5.3+ 基本守护进程,适用于创建长时间运行的 PHP 脚本。
1.0.0
2014-02-12 16:38 UTC
Requires
- php: >=5.3.0
- ext-pcntl: *
Requires (Dev)
- php: >=5.3.0
- ext-pcntl: *
Suggests
- ext-proctitle: Allows setting the current process name on Linux and BSD
This package is not auto-updated.
Last update: 2022-02-01 12:31:12 UTC
README
关于
Sweelix\daemon 是一个用于创建长时间运行的 PHP 脚本的 PHP 5.3+ 库。最新的类 API 文档 可在线查看。
需求
此小程序依赖于
- ext-pcntl : 进程控制 PHP 扩展
- ext-posix : PHP Posix 扩展
还建议安装
- ext-proctitle : 允许更改进程名称的扩展
示例
最无用的守护进程
<?php /** * File Writed.php * * PHP version 5.3+ * * @author Philippe Gaultier <pgaultier@sweelix.net> * @copyright 2010-2014 Sweelix * @license http://www.sweelix.net/license license * @version 1.0.0 * @link http://www.sweelix.net * @category demo * @package sweelix.demo */ use sweelix\daemon\Daemon; /** * Writed is a useless daemon which write dots in the CLI. * This daemon is just here to demonstrate how it can work. * @see sweelix/daemon * * @author Philippe Gaultier <pgaultier@sweelix.net> * @copyright 2010-2014 Sweelix * @license http://www.sweelix.net/license license * @version 1.0.0 * @link http://www.sweelix.net * @category demo * @package sweelix.demo */ class Writed extends Daemon { /** * @var integer var used to count the number of dots to display */ private $_loopNum=0; /** * Init our daemon. Here we do nothing except showing we are starting * @see \sweelix\daemon\Daemon::setUp() * * @return void * @since 1.0.0 */ public function setUp() { $this->write('Warming up engine'."\n"); } /** * Clean up our daemon. Here we do nothing except showing we are stopping * @see \sweelix\daemon\Daemon::tearDown() * * @return void * @since 1.0.0 */ public function tearDown() { $this->write('Cool down the engine'."\n"); } /** * The task we are performing on each loop * We are only writing dots on the screen * @see \sweelix\daemon\Daemon::task() * * @return void * @since 1.0.0 */ public function task() { $this->_loopNum++; sleep(1); $this->write('.'); if($this->_loopNum >= 10) { $this->_loopNum = 0; $this->write("\n"); } } }
安装
首选的安装方法是通过 [Packagist][] 和 [Composer][]。运行以下命令安装软件包并将它添加到 composer.json
的需求中
composer.phar require sweelix/daemon=1.0.0
运行
现在我们可以运行它了
<?php /** * File test.php * * PHP version 5.3+ * * @author Philippe Gaultier <pgaultier@sweelix.net> * @copyright 2010-2014 Sweelix * @license http://www.sweelix.net/license license * @version 1.0.0 * @link http://www.sweelix.net * @category demo * @package sweelix.demo */ /** * include composer autoloader */ require('vendor/autoload.php'); require('Writed.php'); $daemon = new Writed(); // Run it $daemon->run();