openswoole / ext-openswoole

Programmatic server for PHP with async IO, coroutines and fibers
https://openswoole.com
Apache License 2.0
801 stars 50 forks source link

Really struggling how to use MySQL with OpenSwoole #318

Closed ghnp5 closed 11 months ago

ghnp5 commented 1 year ago

Hi,

I attempted to use the "mysqli" extension directly, but unfortunately I started getting plenty of random 408 responses (nginx returns 408 immediately), even though the requests seem to process successfully (all the data being inserted in the database, etc).

I believe this is because I'm using "mysqli" in a blocking way, and I should be using Coroutines.

However, I'm really struggling trying to understand how to use them, and the documentation doesn't seem to be clear at all.

For example, this: https://openswoole.com/docs/modules/swoole-coroutine-mysql

Where does co::set and co::run come from?
And go?
How do I import those methods??

I tried use OpenSwoole\Coroutine, use OpenSwoole\Core\Coroutine, use OpenSwoole\Coroutine as Co;, and a few other things, but it doesn't seem to be defined anywhere!

I have OpenSwoole\Core installed by Composer, and I'm using the pecl openswoole extension.

I also tried something like:

use OpenSwoole\Coroutine as Co;
...
$server->on('Request', function (Request $request, Response $response) use ($server) : void {
    ...
    co::run(function() {
        ...
    }

    $response->end(json_encode_x($output));
}

But then I get:

Server::check_worker_exit_status(): worker(pid=3286, id=42) abnormal exit, status=0, signal=11 A bug occurred in OpenSwoole-v22.0.0, please report it. Please submit bug report at: https://github.com/openswoole/swoole-src/issues

OS: Linux 3.10.0-1160.90.1.el7.x86_64 #1 SMP Thu May 4 15:21:22 UTC 2023 x86_64 GCC_VERSION: 10.2.1 20210110 PHP_VERSION : 8.2.0

and the client never gets a response.

(I'm using OpenSwoole under Docker btw)

--

Thank you very much.

doubaokun commented 11 months ago

Please check example at: https://github.com/openswoole/openswoole/blob/master/example/src/Coroutine/MySQLClientPool.php