PHP code example of icy8 / process

1. Go to this page and download the library: Download icy8/process library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

icy8 / process example snippets


    
    use icy8\process\Worker;
    $handle        = new Worker();
    $handle->total = 2;// 需要运行的进程数
    // $handle->max = 10;// 允许同一时间打开的进程数上限 0为无上限;通常建议设定一个大于零的数值
    $handle->run(function ($worker) {
        // 闭包里是你的业务代码    
        while (1) {
            var_dump(time());
            sleep(1);
        }
    });
    

     
    use icy8\process\Worker;
    $handle        = new Worker();
    $processes     = [
        function () {
            while (1) {
                var_dump('process 1');
                sleep(1);
            }
        },
        function () {
            while (1) {
                var_dump('process 2');
                sleep(1);
            }
        },
        function () {
            while (1) {
                var_dump('process 3');
                sleep(1);
            }
        },
        function () {
            while (1) {
                var_dump('process 4');
                sleep(1);
            }
        },
    ];
    // 此时你不再需要手动配置$handle->total属性
    $handle->run($processes);
    

    
    use icy8\process\Daemon;
    $daemo = new Daemon();
    $daemon->run(function () {    
        sleep(10);        
    });
    

    
    use icy8\process\Daemon;
    $daemon = new Daemon();
    $daemon->start();
    // 所有业务逻辑写在下面
    // 此时的脚本文件不再自动支持 --start|stop|status参数
    $i = 0;
    while($i < 10) {
        $i++;
        var_dump($i);
    }
    

    
    use icy8\process\Worker;
    use icy8\process\Daemon;
    
    $daemon = new Daemon();
    //$daemon->pidFile = '/www/test.pid';// 允许自定义一个存放进程id的文件路径,一定要设置绝对路径
    $daemon->run(function () {
        // 放到守护进程运行
        $handle        = new Worker();
        $handle->total = 2;
        $handle->run(function ($worker) {
            while (1) {
                var_dump(time());
                sleep(1);
            }
        });
    });