Closed ParkFramework closed 8 years ago
We use a simpler API - $result = yield $db->execAsync($sql)
, but it requires an asynchronous PHP framework.
Here is an example https://github.com/icicleio/postgres/blob/master/examples/test.php
Looks interesting!
Looks interesting!
I think useful to do with API integration to EventLoop (Ev, Event, etc..), I'll write later working examples to see how best to make of integration to EventLoop.
Sample php userland:
// runtime before async...
$result = yield $db->execAsync($sql); // pause runtime
// runtime after async...
This solves the problem of queues because the code is executed sequentially and async.
You did not think to move all *async
methods to new class pq\ConnectionAsync
, so it would be nice?
$db = new pq\ConnectionAsync($dsn) // async connection
$db->exec($sql); // execAsync
$db->prepare($sql); // prepareAsync
$db->listen($sql); // listenAsync
//...
I think to make it truly concurrent, I'd have to yield
from within ext-pq, which does not work.
I'm busy now, this all implementing in userland, i later show you :)
$result = parent::exec($sql); // Must be in reality - async work in PQ
AFAICT I'd have to yield in there, else it would not be really asynchronous/concurrent.
AFAICT I'd have to yield in there, else it would not be really asynchronous/concurrent.
$result = yield $db->execAsync($sql); // userland framework - save this generator in global var $coroutine
//...
$loop = EvLoop::defaultLoop();
$io = $loop->io($db->socket, Ev::READ, function($w) use($db)
{
global $coroutine; // this last generator
$db->poll();
$r = $db->getResult();
if($r === null)
{
return;
}
if($r->status === pq\Result::TUPLES_OK)
{
$coroutine->send($r);
}
else
{
$coroutine->throw(new Exception($r->errorMessage));
}
});
$loop->run();
I done working example, pq asynchronous calls, via coroutine, like async/await
https://github.com/ParkFramework/pq-async/blob/master/src/
Use eventloop - EvIo https://github.com/ParkFramework/pq-async/blob/master/src/DB.php#L23
I'm picking this up again in amphp and I will have a public working implementation in the next week or two.
TL;DR having co-routine support directly in the extension doesn't make a lot of sense until it exists in the language as a whole (using generators is a hack, a working hack but a hack nontheless). Callback-driven APIs can be adapted to work with co-routines via the promise pattern.
I agree that this would be a nice thing to have directly supported by the extension API, but now is not the right time.
IMHO.
I agree that this would be a nice thing to have directly supported by the extension API, but now is not the right time.
Yes, i agree.
I think you can close, up the best times and ideas :)
The use of callbacks, there are disadvantages
Callback Hell :) http://callbackhell.com/
In other languages, it is easy to solve,
async/await
-$result = await $db->exec($sql)
, when there will be this in PHP is not known.Here's what I suggest, is the draft, but it works:
Print
What do you think about the use coroutine?
This does not solve the problem of independent queries queue, this for only related queries or single query.
Thank.