lox / pheasant

A lightweight data mapper designed to take advantage of PHP 5.3+
http://getpheasant.com
MIT License
101 stars 21 forks source link

Async query support #69

Closed dhotson closed 10 years ago

dhotson commented 11 years ago

Hi Lox,

This is a proof of concept for async queries.

Here's what it looks like in practice:

// Set up an extra connection to do async stuff on
\Pheasant::instance()->connections()->addConnection('async1', $dsn);

$results = Animal::all()
    ->filter('!sleep(1)') // Pretend this query is slow (sleeps 1 second per row)
    ->async('async1')
    ;

// do some stuff in the meantime..

foreach ($results as $a) // Blocks until results are ready
  var_dump($a->name);

Basically, I've added a async method to the Collection class to kick off a query in the background on a specific connection. It blocks when it needs the result.

The idea would be to have a small pool of connections available for doing queries in the background. It's currently all very explicit so that there's no surprises.

The test case I've included in this pull request demonstrates the feature quite well, it's able to do 3 seconds of work in PHP while two 3sec and 6sec mysql queries run in the background in parallel. the entire test case runs in 6sec rather than 12sec if it were to run sequentially.

Pretty neat huh? :-)

lox commented 10 years ago

I'm closing this, as I plan to move to PDO in v2.