Skip to content

Swoole 4.6.1、Swoole 5.1.6、Swoole 6.0.0 验证协程客户端PgSql 无异步效果(Swoole 4.6.1, Swoole 5.1.6, Swoole 6.0.0 verify that the coroutine client PgSql has no asynchronous effect) #5635

Closed
@yibin94

Description

@yibin94

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error.

Hyperf2.2项目,Swoole版本是4.8.11,需要使用协程客户端的PgSql,4.x版本需要额外编译swoole/ext-postgresql(最新版是v4.8.0),
于是另外找了个4.6.1的版本去验证协程效果,Swoole和ext-postgresql版本信息如下:
验证得出结论:4.6.1、5.1.6、6.0.0 都没有协程PgSql的异步效果,是同步阻塞,希望能得到解答,确认是我这边哪里疏忽还是说是bug

code: (Swoole 4.6.1、Swoole 5.1.6)

<?php

use Swoole\Coroutine;
use Swoole\Coroutine\PostgreSQL;
use function Swoole\Coroutine\run;

run(function () {
    for ($i = 1;$i <= 2;$i++) {
       Coroutine::create(function () use ($i) {
           $pg = new PostgreSQL();
           $conn = $pg->connect(sprintf(
              'host=%s port=%s dbname=%s user=%s password=%s',
              'xxx',
               5432,
              'xxx',
              'xxx',
              'xxx'
           ));
           if (!$conn) {
             var_dump($pg->error);
             return;
           }
           $stmt = $pg->query('SELECT * FROM tab  limit 3;');
           $arr = $pg->fetchAll($stmt);
           var_dump($i . ' :rows num=' . count($arr));
        });
   }
});
var_dump(666);
sleep(8);
var_dump(888);

code: (Swoole 5.1.6、Swoole 6.0.0)

<?php

use Swoole\Coroutine;
use function Swoole\Coroutine\run;

Coroutine::set(['hook_flags' => SWOOLE_HOOK_ALL]);
Coroutine::set(['trace_flags' => SWOOLE_HOOK_PDO_PGSQL]);
run(function () {
    for ($i = 1;$i <= 2;$i++) {
      Coroutine::create(function () use ($i) {
        $conn = new PDO(sprintf(
            "pgsql:dbname=%s;host=%s;port=%s",
            'xxx',
            'xxx',
            5432),
            'xxx',
            'xxx');
        if (!$conn) {
            var_dump($conn->error);
            return;
        }

        $stmt = $conn->query('SELECT * FROM tab limit 3;');
        $arr = $stmt->fetchAll();
        var_dump($i . ' :rows num=' . count($arr));
      });
    }
});
var_dump(666);
sleep(8);
var_dump(888);

  1. What did you expect to see?
    遇到数据库查询,先切换到外部协程打印数字,而不是一直阻塞等数据库查询返回结果。

  2. What did you see instead?

   string(13) "1 :rows num=3"
   string(13) "2 :rows num=3"
   int(666)
   int(888)
  1. What version of Swoole are you using (show your php --ri swoole)?

Swoole 4.6.1

Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 4.6.1
Built => Jan 11 2021 12:29:13
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 1.1.1i  8 Dec 2020
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
pcre => enabled
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => Off => Off
swoole.unixsock_buffer_size => 8388608 => 8388608

php --ri swoole_postgresql

swoole_postgresql

Swoole PostgreSQL => enabled
Author => Swoole Team <[email protected]>
Version => 4.6.1
Built => Sep 17 2021 09:55:41
trace_log => enabled

Swoole 5.1.6 (dockerfile)

Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 5.1.6
Built => Dec 24 2024 02:51:43
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
spinlock => enabled
rwlock => enabled
sockets => enabled
openssl => OpenSSL 1.1.1w  11 Sep 2023
dtls => enabled
http2 => enabled
json => enabled
curl-native => enabled
pcre => enabled
zlib => 1.2.12
brotli => E16777225/D16777225
mutex_timedlock => enabled
pthread_barrier => enabled
mysqlnd => enabled
async_redis => enabled
coroutine_pgsql => enabled

Swoole 6.0.0 (dockerfile)

Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 6.0.0
Built => Dec 24 2024 05:19:00
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
signalfd => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 3.3.2 3 Sep 2024
dtls => enabled
http2 => enabled
json => enabled
mutex_timedlock => enabled
pthread_barrier => enabled
coroutine_pgsql => enabled

Directive => Local Value => Master Value
swoole.enable_library => On => On
swoole.enable_fiber_mock => Off => Off
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608
  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ?
   Swoole 4.6.1 环境下对应 php= PHP 7.4.13 (cli) (built: Dec 28 2020 05:26:43) ( NTS )
   Swoole 5.1.6 环境下对应 php = PHP 8.0.30 (cli) (built: Oct 21 2023 06:13:34) ( NTS )
   Swoole 6.0.0 环境下对应 php = PHP 8.1.31 (cli) (built: Dec 11 2024 23:33:39) (NTS)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions