dev-resources.site
for different kinds of informations.
PHP HyperF + MariaDB -> Async / Parallel
Published at
6/25/2024
Categories
php
hyperf
mariadb
async
Author
thiagoeti
Author
9 person written this
thiagoeti
open
- PHP: 8.3.7
- PHP HyperF: 3.1.23
- MariaDB: 11.3.2
HyperF - Project
System test for executing database queries in parallel or asynchronously.
Create - Project
composer create-project hyperf/hyperf-skeleton "project"
Install - Watcher
composer require hyperf/watcher --dev
Install - DB Library
composer require hyperf/database
composer require hyperf/db-connection
Server - Start
cd project ;
php bin/hyperf.php server:watch ;
MariaDB - Database
The data model is in meta tables, just for example.
Database - Project
CREATE DATABASE `project`;
Table - Family
CREATE TABLE `family` (
`id` tinyint(3) unsigned NOT NULL,
`name` varchar(40) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Table - Pets
CREATE TABLE `pet` (
`id` tinyint(3) unsigned NOT NULL,
`name` varchar(40) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Table - Car
CREATE TABLE `car` (
`id` tinyint(3) unsigned NOT NULL,
`name` varchar(40) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Table - Family Setting
CREATE TABLE `family_setting` (
`id_family` tinyint(3) unsigned NOT NULL,
`type` varchar(10) DEFAULT NULL,
`key` tinyint(3) unsigned NOT NULL,
KEY (`id_family`),
KEY (`type`),
KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Populate - ALL
INSERT INTO `family` (`id`, `name`) VALUES(1, 'Family 1');
INSERT INTO `family` (`id`, `name`) VALUES(2, 'Family 2');
INSERT INTO `family` (`id`, `name`) VALUES(3, 'Family 3');
INSERT INTO `pet` (`id`, `name`) VALUES(1, 'Pet 1');
INSERT INTO `pet` (`id`, `name`) VALUES(2, 'Pet 2');
INSERT INTO `pet` (`id`, `name`) VALUES(3, 'Pet 3');
INSERT INTO `car` (`id`, `name`) VALUES(1, 'Car 1');
INSERT INTO `car` (`id`, `name`) VALUES(2, 'Car 2');
INSERT INTO `car` (`id`, `name`) VALUES(3, 'Car 3');
INSERT INTO `family_setting` (`id_family`, `type`, `key`) VALUES(1, 'pet', 1);
INSERT INTO `family_setting` (`id_family`, `type`, `key`) VALUES(1, 'pet', 2);
INSERT INTO `family_setting` (`id_family`, `type`, `key`) VALUES(1, 'car', 3);
INSERT INTO `family_setting` (`id_family`, `type`, `key`) VALUES(2, 'car', 1);
INSERT INTO `family_setting` (`id_family`, `type`, `key`) VALUES(2, 'car', 2);
INSERT INTO `family_setting` (`id_family`, `type`, `key`) VALUES(3, 'pet', 3);
HyperF - APP
Database Config - MariaDB
DB_DRIVER=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=project
DB_USERNAME=root
DB_PASSWORD=master
path: /project/.env
APP - Router
Router::addRoute(['GET', 'POST'], '/family', 'App\Controller\ControllerFamily@data');
Router::addRoute(['GET', 'POST'], '/family/mount', 'App\Controller\ControllerFamily@mount');
path: /project/config/routes.php
APP - Model - Family
namespace App\Model;
use Hyperf\DbConnection\Db;
class ModelFamily extends Model
{
static public function data()
{
$family=Db::select('SELECT * FROM `family`;');
return $family;
}
static public function mount()
{
$parallel=new \Hyperf\Coroutine\Parallel();
$parallel->add(function(){
$data=Db::select('SELECT * FROM `family`;');
return $data;
});
$parallel->add(function(){
$data=Db::select('SELECT * FROM `pet`;');
sleep(2); // important add time test
return $data;
});
$parallel->add(function(){
$data=Db::select('SELECT * FROM `car`;');
sleep(1); // important add time test
return $data;
});
$parallel->add(function(){
$data=Db::select('SELECT * FROM `family_setting`;');
return $data;
});
$result=$parallel->wait();
$result=[
'family'=>$result[0],
'pet'=>$result[1],
'car'=>$result[2],
'family_setting'=>$result[3],
];
return $result;
}
}
path: /project/app/Model/ModelFamily.php
APP - Controller - Family
namespace App\Controller;
use \App\Model\ModelFamily;
class ControllerFamily
{
public function data()
{
$family=ModelFamily::data();
return $family;
}
public function mount()
{
$data=ModelFamily::mount();
return $data;
}
}
path: /project/app/Controller/ControllerFamily.php
Execute
GET - Family
curl "http://127.0.0.1:9501/family" | jq .
[
{
"id": 1,
"name": "Family 1"
},
{
"id": 2,
"name": "Family 2"
},
{
"id": 3,
"name": "Family 3"
}
]
GET - Mount
curl "http://127.0.0.1:9501/family/mount" | jq .
{
"family": [
{
"id": 1,
"name": "Family 1"
},
...
],
"pet": [
{
"id": 1,
"name": "Pet 1"
},
...
],
"car": [
{
"id": 1,
"name": "Car 1"
},
...
],
"family_setting": [
{
"id_family": 1,
"type": "pet",
"key": 1
},
{
"id_family": 1,
"type": "pet",
"key": 2
},
...
]
}
https://github.com/thiagoeti/php-hyperf-mariadb-async-parrallel
async Article's
30 articles in total
This Small Python Script Improved Understanding of Low-Level Programming
read article
Async,Await Promise
read article
Async Vs Sync, which is most preferrable?
read article
Async/Await: Task.WhenAll + Exceptions = Dor de Cabeça!
read article
Everything You Need to Know About JavaScript Promises and How They Work
read article
Asynchronous Python
read article
Building pipelines with IAsyncEnumerable in .NET
read article
Unleash the Power of FastAPI: Async vs Blocking I/O
read article
Total Madness #2: Async Locks
read article
Don't use 'BuildContext's across async gaps.
read article
Integration Digest: May 2024
read article
Total Madness #1: Async/Await
read article
Forcing Angular SSR to Wait in 2024
read article
Using Async in Ruby on Rails for CSV export
read article
Mastering Async Await in JavaScript for Asynchronous Programming
read article
PHP HyperF + MariaDB -> Async / Parallel
currently reading
Async/await and SwiftUI
read article
🕒 Task vs Promise: Chaining
read article
🕒 Task vs Promise: Encadenación
read article
New custom blocks for Analytics Builder (async comms, downsampling and complex measurements)
read article
Concurrent-ruby (async) S3 files download
read article
Ruby class pattern to work with API requests with built-in async approach
read article
How to use ActionCable with async requests in a Ruby on Rails web app
read article
Introducing EventSail: A Python Library for Event-driven Programming
read article
Enhancing Asynchronous Data Fetching in Umbraco v14 with Lit Async Directives
read article
API simples que gera arquivos de forma assÃncrona, com Java e Spring? Aqui tem!
read article
Async Axiom logging
read article
Rust: Actix-web -- Async Functions as Middlewares
read article
Streamlining Asynchronous Tasks in Django with Django Tasks Scheduler
read article
JavaScript async call analysis
read article
Featured ones: