thinkphp5.0 use workerman multithreaded examples

Test environment: php7 + nginx + linux

  

First Quguan network download workerman: https://www.workerman.net/ , composer recommended installation

 

Create a new module, because I used here workerman do automated tasks, so I name this module is: task

 

Create a folder inside the controller in the controller: Worker.php, which code is as follows

<?php
namespace app\task\controller;
use think\worker\Server;
use Workerman\Lib\Timer;

class Worker extends Server {
    protected $socket = '';
    protected $ processes = 4; // four process
    protected $ port = '2369'; // listening port

    /**
     * Each process starts
     * @param $worker
     */
    public function onWorkerStart($worker){
        if ($worker->id === 0) {
            Timer::add(1,function(){
                echo 'This is the process .... 0';
            });            
        }else if ($worker->id === 1) {
            Timer::add(1, function(){
                echo 'This is a process 1 .....';
            });
        }else if ($worker->id === 2) {
            Timer::add(1, function() {
                echo 'This is the process 2. Process 2 ...';
            });
        }else if ($worker->id === 3) {
            Timer::add(1, function() {
                echo '3 process, which is the process .... 3';
            });
        }
    }
}

  Add a file entry worker.php, as follows:

<?php
define('APP_PATH', __DIR__ . '/../app/');
define('BIND_MODULE','task/Worker');
// Load File framework guide
require __DIR__ . '/../thinkphp/start.php';

  

 Finally, the execution cli mode button above the entrance to file (window system to use cmd)

 

Guess you like

Origin www.cnblogs.com/sky-yu/p/11723773.html