PHP code example of swoole-foundation / swoole-pool

1. Go to this page and download the library: Download swoole-foundation/swoole-pool library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

swoole-foundation / swoole-pool example snippets


    
    $pool = new GenericPool(10, function () {
            $connection = new MySQL();
            $connection->connect([
                'host' => '127.0.0.1',
                'port' => 3306,
                'user' => 'root',
                'password' => 'root',
                'database' => 'test',
            ]);
            if (!$connection->connected) {
                throw new Exception($connection->connect_error, $connection->errno);
            }
            return $connection;
        });
    
    
    for ($i = 0; $i < 100; $i++) {
        go(function () use ($pool, $i) {
            /** @var MySQL $connection */
            $connection = $pool->acquire();
            defer(function () use ($pool, $connection) {
                $pool->release($connection);
            });
    
            $data = $connection->query('SELECT CONNECTION_ID() AS `id`');
            print_r($data);
        });
    }