PHP HyperF -> Overlapping and Concurrency

王林
Release: 2024-07-17 03:01:20
Original
1041 people have browsed it

PHP HyperF -> Overlapping and Concurrency Overlapping and Concurrency" />

  • PHP: 8.3.7
  • PHP HyperF: 3.1.23

HyperF - Project

It is extremely important that HyperF processes do not overwrite shared variable data. This can occur with overload.

Create - Project

composer create-project hyperf/hyperf-skeleton "project"
Copy after login

Install - Watcher

composer require hyperf/watcher --dev
Copy after login

Install - UUID

composer require ramsey/uuid
Copy after login

Server - Start

cd project ;
php bin/hyperf.php server:watch ;
Copy after login

HyperF - APP

APP - Router

Router::addRoute(['GET', 'POST'], '/overlapping/data', 'App\Controller\ControllerOverlapping@data');
Router::addRoute(['GET', 'POST'], '/overlapping/value-a', 'App\Controller\ControllerOverlapping@value_a');
Router::addRoute(['GET', 'POST'], '/overlapping/value-b', 'App\Controller\ControllerOverlapping@value_b');
Router::addRoute(['GET', 'POST'], '/overlapping/value-c', 'App\Controller\ControllerOverlapping@value_c');

Router::addRoute(['GET', 'POST'], '/concurrency/data', 'App\Controller\ControllerConcurrency@data');
Router::addRoute(['GET', 'POST'], '/concurrency/value-a', 'App\Controller\ControllerConcurrency@value_a');
Router::addRoute(['GET', 'POST'], '/concurrency/value-b', 'App\Controller\ControllerConcurrency@value_b');
Router::addRoute(['GET', 'POST'], '/concurrency/value-c', 'App\Controller\ControllerConcurrency@value_c');
Copy after login

path: /project/config/routes.php

APP - Controller - Overlapping

namespace App\Controller;

class ControllerOverlapping
{
    public $data;

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

    public function data()
    {
        return $this->data;
    }

    public function value_a()
    {
        $this->data='Value A';
        return $this->data;
    }

    public function value_b()
    {
        $this->data='Value B';
        return $this->data;
    }

    public function value_C()
    {
        $this->data='Value C';
        return $this->data;
    }

}
Copy after login

path: /project/app/Controller/ControllerOverlapping.php

APP - Controller - Control

namespace App\Controller;

use Ramsey\Uuid\Uuid;

class ControllerConcurrency
{
    public $data;

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

    public function start()
    {
        $key=Uuid::uuid4()->toString();
        return $key;
    }

    public function end($key)
    {
        $data=$this->data[$key];
        unset($this->data[$key]);
        return $data;
    }

    public function data()
    {
        return $this->data;
    }

    public function value_a()
    {
        $key=$this->start();
        $this->data[$key]='Value A';
        $data=$this->end($key);
        return $data;
    }

    public function value_b()
    {
        $key=Uuid::uuid4()->toString();
        $this->data[$key]='Value B';
        $data=$this->end($key);
        return $data;
    }

    public function value_C()
    {
        $key=Uuid::uuid4()->toString();
        $this->data[$key]='Value C';
        $data=$this->end($key);
        return $data;
    }

}
Copy after login

path: /project/app/Controller/ControllerConcurrency.php

Execute

GET - Concurrency

curl "http://127.0.0.1:9501/overlapping/data"
Result: ''

curl "http://127.0.0.1:9501/overlapping/value-a"
Result: 'Value A'

curl "http://127.0.0.1:9501/overlapping/data"
Result: 'Value A'

curl "http://127.0.0.1:9501/overlapping/value-b"
Result: 'Value B'

curl "http://127.0.0.1:9501/overlapping/data"
Result: 'Value B'
Copy after login

GET - Control

curl "http://127.0.0.1:9501/concurrency/data"
Result: ''

curl "http://127.0.0.1:9501/concurrency/value-a"
Result: 'Value A'

curl "http://127.0.0.1:9501/concurrency/data"
Result: ''

curl "http://127.0.0.1:9501/concurrency/value-b"
Result: 'Value B'

curl "http://127.0.0.1:9501/concurrency/data"
Result: ''

curl "http://127.0.0.1:9501/concurrency/value-a" && \
curl "http://127.0.0.1:9501/concurrency/value-a" && \
curl "http://127.0.0.1:9501/concurrency/value-c" ;
Result: 'Value A', 'Value B', 'Value C'

curl "http://127.0.0.1:9501/concurrency/data"
Result: ''
Copy after login

https://github.com/thiagoeti/php-hyperf-overlapping-concurrency

The above is the detailed content of PHP HyperF -> Overlapping and Concurrency. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!