Skip to content

Instantly share code, notes, and snippets.

@ThomasWeinert
Created May 30, 2013 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasWeinert/5677795 to your computer and use it in GitHub Desktop.
Save ThomasWeinert/5677795 to your computer and use it in GitHub Desktop.
Asynchronous MySQL queries
<?php
include('../../src/Carica/Io/Loader.php');
Carica\Io\Loader::register();
use Carica\Io;
$mysqlOne = new Io\Deferred\MySQL(new mysqli('127.0.0.1'));
$mysqlTwo = new Io\Deferred\MySQL(new mysqli('localhost'));
$time = microtime(TRUE);
$queries = Io\Deferred::When(
$mysqlOne("SELECT 'Query 1', SLEEP(5)")
->done(
function($result) use ($time) {
var_dump(iterator_to_array($result));
var_dump(microtime(TRUE) - $time);
}
),
$mysqlTwo("SELECT 'Query 2', SLEEP(2)")
->done(
function($result) use ($time) {
var_dump(iterator_to_array($result));
var_dump(microtime(TRUE) - $time);
}
)
);
$loop = Io\Event\Loop\Factory::get();
Io\Event\Loop\Factory::run($queries);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment