Home > PHP Framework > Workerman > body text

How to use Workerman to implement a distributed computing system

WBOY
Release: 2023-11-08 14:19:51
Original
1535 people have browsed it

How to use Workerman to implement a distributed computing system

Distributed computing system refers to a computing model that treats a group of computers as a single system to collaboratively complete computing tasks. In practice, distributed computing systems can increase the computing speed by increasing the number of computers, and at the same time can solve the problem of processing large amounts of data. Workerman is a framework that can implement distributed computing systems using PHP language. This article will introduce how to use Workerman to implement a simple distributed computing system and provide code examples.

  1. Install Workerman

First, we need to install Workerman. It can be installed through Composer. The specific command is as follows:

composer require workerman/workerman
Copy after login
  1. Create server program

We will create a server program named server.php and run With this program, the client can submit computing tasks to the server, and the server is responsible for assigning tasks to computing nodes for calculation and returning the final results to the client. The following is a code example of server.php:

<?php
use WorkermanWorker;

require_once __DIR__ . '/vendor/autoload.php';

$worker = new Worker('text://0.0.0.0:2346');

$worker->count = 4;

$worker->onMessage = function($connection, $data){
    $params = json_decode($data, true);

    $worker_num = $params['worker_num'];
    $task_data = $params['task_data'];

    $task_id = md5($task_data);

    $task_worker = new Task($task_id);
    $task_worker->send([
        'worker_num' => $worker_num,
        'task_data' => $task_data
    ]);
  
    $connection->send(json_encode([
        'task_id' => $task_id
    ]));
};

class Task{
    protected $task_id;
  
    protected $worker_num;

    protected $task_data;

    public function __construct($task_id){
        $this->task_id = $task_id;
    }

    public function send($data){
        $task_data = json_encode([
            'task_id' => $this->task_id,
            'data' => $data
        ]);

        $worker_num = $data['worker_num'];
        $socket_name = "tcp://127.0.0.1:".(2347 + $worker_num);

        $client = stream_socket_client($socket_name, $errno, $errstr);
  
        fwrite($client, $task_data);
        fclose($client);
    }
}

Worker::runAll();
Copy after login

In the above code, we use the server listening port to wait for the client to submit a task. When the server receives the task submitted by the client, the server will assign the task to a computing node for calculation and return the results to the client.

In the instance of the Worker class, we configured 4 processes to handle client requests. In the onMessage event callback, we first obtain worker_num and task_data from the JSON data submitted by the client, then create a new Task instance, send the task to the computing node, and wait for the calculation result to be returned.

In the Task class, we store the task ID (task_id), the node number to be calculated (worker_num) and the data to be calculated (task_data). The send() method is used to send tasks to the specified computing node. Here, we use the stream_socket_client() function to implement a TCP socket client for communicating with the specified computing node.

  1. Create a computing node program

Next, we create a computing node program named worker.php. The program will perform calculations after the server assigns the calculation tasks to it, and return the results to the server. The following is a code example for worker.php:

<?php
use WorkermanWorker;

require_once __DIR__ . '/vendor/autoload.php';

$worker_num = intval($argv[1]);

$worker = new Worker("tcp://0.0.0.0:". (2347 + $worker_num));

$worker->onMessage = function($connection, $data){
    $params = json_decode($data, true);
    $task_id = $params['task_id'];

    $task_data = $params['data'];

    $result = strlen($task_data);

    $connection->send(json_encode([
        'task_id' => $task_id,
        'result' => $result
    ]));
};

Worker::runAll();
Copy after login

In the above code, we use a TCP socket to listen to a port and wait for the server to assign computing tasks. When there is a computing task that needs to be processed, we obtain the data that needs to be processed from the task data, perform calculations, and send the results to the server.

  1. Create client program

Finally, we need to create a client program named client.php to submit calculation tasks to the server and obtain calculations result. The following is a code example for client.php:

<?php
use WorkermanWorker;

require_once __DIR__ . '/vendor/autoload.php';

$client = stream_socket_client("tcp://127.0.0.1:2346", $errno, $errstr);

$data = [
    'worker_num' => 1,
    'task_data' => 'Workerman is a high-performance PHP socket framework'
];

$json_data = json_encode($data);

fwrite($client, $json_data);
$result = fread($client, 8192);

fclose($client);

$result_data = json_decode($result, true);

$task_id = $result_data['task_id'];

foreach(range(0,3) as $worker_num){
    $worker_client = stream_socket_client("tcp://127.0.0.1:". (2347 + $worker_num), $errno, $errstr);

    fwrite($worker_client, json_encode([
        'task_id' => $task_id,
        'worker_num' => $worker_num
    ]));

    $worker_result = fread($worker_client, 8192);

    $worker_result_data = json_decode($worker_result, true);

    if($worker_result_data['task_id'] == $task_id){
        echo "Result: " . $worker_result_data['result'] . PHP_EOL;
        break;
    }
}
Copy after login

In the above code, we first create a TCP socket client to connect to the compute node. The fread() function is used here to obtain the return results of the calculation task from the server. Then we send task_id as a parameter to all computing nodes and wait for the results to be returned. Based on the task ID (task_id), we can identify which computing node returned the calculation result. Finally we can output the calculation results.

Summary

The above are the detailed steps on how to use Workerman to implement a distributed computing system, including creating server programs, computing node programs and client programs, and providing specific code examples. It is worth mentioning that the examples provided in this article only demonstrate the basic ideas of how to use Workerman to implement distributed computing systems. There are still some problems in practical applications, such as load balancing, task allocation strategies, etc. But all of these problems can be solved by carefully studying the Workerman framework and adjusting the code.

The above is the detailed content of How to use Workerman to implement a distributed computing system. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template