Logo

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
Categories
4 categories in total
php
open
hyperf
open
mariadb
open
async
open
Author
9 person written this
thiagoeti
open
PHP HyperF + MariaDB -> Async / Parallel
  • 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"
Enter fullscreen mode Exit fullscreen mode

Install - Watcher

composer require hyperf/watcher --dev
Enter fullscreen mode Exit fullscreen mode

Install - DB Library

composer require hyperf/database
composer require hyperf/db-connection
Enter fullscreen mode Exit fullscreen mode

Server - Start

cd project ;
php bin/hyperf.php server:watch ;
Enter fullscreen mode Exit fullscreen mode

MariaDB - Database

The data model is in meta tables, just for example.

Database - Project

CREATE DATABASE `project`;
Enter fullscreen mode Exit fullscreen mode

Table - Family

CREATE TABLE `family` (
    `id` tinyint(3) unsigned NOT NULL,
    `name` varchar(40) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Enter fullscreen mode Exit fullscreen mode

Table - Pets

CREATE TABLE `pet` (
    `id` tinyint(3) unsigned NOT NULL,
    `name` varchar(40) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Enter fullscreen mode Exit fullscreen mode

Table - Car

CREATE TABLE `car` (
    `id` tinyint(3) unsigned NOT NULL,
    `name` varchar(40) DEFAULT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

HyperF - APP

Database Config - MariaDB

DB_DRIVER=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=project
DB_USERNAME=root
DB_PASSWORD=master
Enter fullscreen mode Exit fullscreen mode

path: /project/.env

APP - Router

Router::addRoute(['GET', 'POST'], '/family', 'App\Controller\ControllerFamily@data');
Router::addRoute(['GET', 'POST'], '/family/mount', 'App\Controller\ControllerFamily@mount');
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Execute

GET - Family

curl "http://127.0.0.1:9501/family" | jq .
Enter fullscreen mode Exit fullscreen mode
[
    {
        "id": 1,
        "name": "Family 1"
    },
    {
        "id": 2,
        "name": "Family 2"
    },
    {
        "id": 3,
        "name": "Family 3"
    }
]
Enter fullscreen mode Exit fullscreen mode

GET - Mount

curl "http://127.0.0.1:9501/family/mount" | jq .
Enter fullscreen mode Exit fullscreen mode
{
    "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
        },
        ...
    ]
}
Enter fullscreen mode Exit fullscreen mode

https://github.com/thiagoeti/php-hyperf-mariadb-async-parrallel

async Article's
30 articles in total
Favicon
This Small Python Script Improved Understanding of Low-Level Programming
Favicon
Async,Await Promise
Favicon
Async Vs Sync, which is most preferrable?
Favicon
Async/Await: Task.WhenAll + Exceptions = Dor de Cabeça!
Favicon
Everything You Need to Know About JavaScript Promises and How They Work
Favicon
Asynchronous Python
Favicon
Building pipelines with IAsyncEnumerable in .NET
Favicon
Unleash the Power of FastAPI: Async vs Blocking I/O
Favicon
Total Madness #2: Async Locks
Favicon
Don't use 'BuildContext's across async gaps.
Favicon
Integration Digest: May 2024
Favicon
Total Madness #1: Async/Await
Favicon
Forcing Angular SSR to Wait in 2024
Favicon
Using Async in Ruby on Rails for CSV export
Favicon
Mastering Async Await in JavaScript for Asynchronous Programming
Favicon
PHP HyperF + MariaDB -> Async / Parallel
Favicon
Async/await and SwiftUI
Favicon
🕒 Task vs Promise: Chaining
Favicon
🕒 Task vs Promise: Encadenación
Favicon
New custom blocks for Analytics Builder (async comms, downsampling and complex measurements)
Favicon
Concurrent-ruby (async) S3 files download
Favicon
Ruby class pattern to work with API requests with built-in async approach
Favicon
How to use ActionCable with async requests in a Ruby on Rails web app
Favicon
Introducing EventSail: A Python Library for Event-driven Programming
Favicon
Enhancing Asynchronous Data Fetching in Umbraco v14 with Lit Async Directives
Favicon
API simples que gera arquivos de forma assíncrona, com Java e Spring? Aqui tem!
Favicon
Async Axiom logging
Favicon
Rust: Actix-web -- Async Functions as Middlewares
Favicon
Streamlining Asynchronous Tasks in Django with Django Tasks Scheduler
Favicon
JavaScript async call analysis

Featured ones: