중복 및 동시성" />
HyperF 프로세스가 공유 변수 데이터를 덮어쓰지 않는 것이 매우 중요합니다. 이는 과부하로 인해 발생할 수 있습니다.
composer create-project hyperf/hyperf-skeleton "project"
composer require hyperf/watcher --dev
composer require ramsey/uuid
cd project ; php bin/hyperf.php server:watch ;
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');
경로: /project/config/routes.php
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; } }
경로: /project/app/Controller/ControllerOverlapping.php
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; } }
경로: /project/app/Controller/ControllerConcurrency.php
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'
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: ''
https://github.com/thiagoeti/php-hyperf-overlapping-concurrency
부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.
Copyright© 2022 湘ICP备2022001581号-3