Dbal: Create an "async" API

Created on 13 Sep 2017  Â·  4Comments  Â·  Source: doctrine/dbal

Guzzle has a really nice "async" API. I put "async" in quotes because it's not really async it's more just _concurency_. curl has support to execute more than one request at a time. Guzzle will group the requests and execute them together, then it will group the next level down and execute them together and so on until all of the requests and callbacks are completed. Something they don't add to the docs is that you must call wait() or the request(s) are never executed (because it's basically just a huge array of requests).

I noticed that PDO::execute allows more than one query to be executed (without a result set), and PDOStatement::nextRowset will allow access to multiple row sets on queries that have multiple result sets.

It kind of looks like PDO will allow concurrent queries just like curl will allow concurrent requests.

I think it would be helpful if there was some sort of "async" API that would return a _Promises/A+_ promise. You'd still have to call wait() just like Guzzle, but it would make it really convenient to group and ungroup queries together so queries can be executed concurrently without making a huge mess.

Most helpful comment

This could be interesting, but won't fit the existing API.

What could be useful is an AsyncConnection interface returning promises
and consuming promises (specifically around transactions).

Can be done, but I doubt it will happen with PDO as underlying layer.

On 13 Sep 2017 04:18, "David Barratt" notifications@github.com wrote:

Guzzle http://docs.guzzlephp.org/en/stable/ has a really nice "async"
API http://docs.guzzlephp.org/en/stable/quickstart.html#async-requests.
I put "async" in quotes because it's not really async it's more just
concurency. curl has support to execute more than one request at a
time. Guzzle will group the requests and execute them together, then it
will group the next level down and execute them together and so on until
all of the requests and callbacks are completed. Something they don't add
to the docs is that you must call wait() or the request(s) are never
executed (because it's basically just a huge array of requests).

I noticed that PDO::execute http://php.net/manual/en/pdo.exec.php
allows more than one query to be executed (without a result set), and
PDOStatement::nextRowset
http://php.net/manual/en/pdostatement.nextrowset.php will allow access
to multiple row sets on queries that have multiple result sets.

It kind of looks like PDO will allow concurrent queries just like curl
will allow concurrent requests.

I think it would be helpful if there was some sort of "async" API that
would return a Promises/A+ promise. You'd still have to call wait()
just like Guzzle, but it would make it really convenient to group and
ungroup queries together so queries can be executed concurrently without
making a huge mess.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/doctrine/dbal/issues/2860, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakPjRHlQvCqhTpXLjaIvFaJvbp2Umks5shztkgaJpZM4PVfWD
.

All 4 comments

Relating this to #2562.

This could be interesting, but won't fit the existing API.

What could be useful is an AsyncConnection interface returning promises
and consuming promises (specifically around transactions).

Can be done, but I doubt it will happen with PDO as underlying layer.

On 13 Sep 2017 04:18, "David Barratt" notifications@github.com wrote:

Guzzle http://docs.guzzlephp.org/en/stable/ has a really nice "async"
API http://docs.guzzlephp.org/en/stable/quickstart.html#async-requests.
I put "async" in quotes because it's not really async it's more just
concurency. curl has support to execute more than one request at a
time. Guzzle will group the requests and execute them together, then it
will group the next level down and execute them together and so on until
all of the requests and callbacks are completed. Something they don't add
to the docs is that you must call wait() or the request(s) are never
executed (because it's basically just a huge array of requests).

I noticed that PDO::execute http://php.net/manual/en/pdo.exec.php
allows more than one query to be executed (without a result set), and
PDOStatement::nextRowset
http://php.net/manual/en/pdostatement.nextrowset.php will allow access
to multiple row sets on queries that have multiple result sets.

It kind of looks like PDO will allow concurrent queries just like curl
will allow concurrent requests.

I think it would be helpful if there was some sort of "async" API that
would return a Promises/A+ promise. You'd still have to call wait()
just like Guzzle, but it would make it really convenient to group and
ungroup queries together so queries can be executed concurrently without
making a huge mess.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/doctrine/dbal/issues/2860, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakPjRHlQvCqhTpXLjaIvFaJvbp2Umks5shztkgaJpZM4PVfWD
.

There is a new PHP extension swoole https://github.com/swoole/swoole-src which has mysql async support out of the box may be worth looking at.

MySQL

Concurrency 10k requests to read data from MySQL takes only 0.2s!

$s = microtime(true);
for ($c = 100; $c--;) {
    go(function () {
        $mysql = new Swoole\Coroutine\MySQL;
        $mysql->connect([
            'host' => '127.0.0.1',
            'user' => 'root',
            'password' => 'root',
            'database' => 'test'
        ]);
        $statement = $mysql->prepare('SELECT * FROM `user`');
        for ($n = 100; $n--;) {
            $result = $statement->execute();
            assert(count($result) > 0);
        }
    });
}
Swoole\Event::wait();
echo 'use ' . (microtime(true) - $s) . ' s';
Was this page helpful?
0 / 5 - 0 ratings