承接 ncrypthic/daemon 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ncrypthic/daemon

Composer 安装命令:

composer require ncrypthic/daemon

包简介

README 文档

README

We often need to implement a long running script in background to do house keeping tasks for our application.

There are couple of techniques that we can use as listed bellow :

  1. With cron by default there are no process control mechanism. If we set a script to run every minute, then it is very that there will be a race condition between process execution when every process of script takes more time than the scheduled time to finish.

  2. When we use one script to execute a collection of tasks in an array using loop. The tasks will be executed one after another. This will make the last task in the collection have to wait for earlier tasks to finish.

What this library do

This library provides basic threaded process control to run our tasks but we want to prevent race condition by running each tasks in order on forked child process using PHP pcntl extension.

Minimum Requirements

  1. PHP >= 5.3.6
  2. Installed and enabled pcntl extension

Installing

  • Via composer
requires: {
    "ncrypthic/daemon": "dev-master"
}

Running

  • Create process(es) classes which implements ProcessInterface interface
<?php
use Ncrypthic\Daemon\Interfaces\ProcessInterface;

class AliceProcess implements ProcessInterface
{
    public function execute()
    {
        sleep(3);
        echo 'Alice done'.PHP_EOL;
        exit;
    }    
}

class BobProcess implements ProcessInterface
{
    public function execute()
    {
        sleep(3);
        echo 'Bob done'.PHP_EOL;
        exit;
    }    
}
  • Create default manager instance and register the tasks
<?php

// ... Other commands

$manager = new \Ncrypthic\Daemon\Manager\DefaultManager();
$manager->addProcess(new AliceProcess());
$manager->addProcess(new BobProcess());
  • Now daemonize the manager
<?php

try {
    $manager->daemonize();
} catch (\Ncrypthic\Daemon\Exception\ChildProcessException $exc) {
    echo $exc->getMessage();
}

Output

  • Console output
bash-4.2$ php <your_script>.php 
Alice done
Bob done
        No child processes
Alice done
Bob done
        No child processes
Alice done
Alice done
Bob done
        No child processes
Alice done
Bob done
        No child processes
Alice done
  • Process manager output
php
   `+-php
    +-php

-- see examples

统计信息

  • 总下载量: 49
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固