Пример #1
0
function genTable(\Amp\Mysql\Pool $db)
{
    (yield $db->query("CREATE TABLE tmp SELECT 1 AS a, 2 AS b"));
    $promises = [];
    foreach (range(1, 5) as $num) {
        $promises[] = $db->query("INSERT INTO tmp (a, b) VALUES ({$num}, {$num} * 2)");
    }
    (yield \Amp\all($promises));
}
Пример #2
0
 function testVirtualConnection()
 {
     $complete = false;
     (new NativeReactor())->run(function ($reactor) use(&$complete) {
         $db = new Pool("host=" . DB_HOST . ";user="******";pass="******";db=connectiontest", null, $reactor);
         /* Multiple queries one after the other must be hold back and dispatched to new connections */
         for ($i = 0; $i < 5; $i++) {
             $pings[] = $db->ping();
         }
         (yield \Amp\all($pings));
         $complete = true;
     });
     $this->assertEquals(true, $complete, "Database commands did not complete.");
 }
Пример #3
0
 private function sumMultipartFieldLengths(array $fields)
 {
     $lengths = [];
     foreach ($fields as $field) {
         if (is_string($field)) {
             $lengths[] = strlen($field);
         } else {
             $lengths[] = $field->getLength();
         }
     }
     $promisor = new Future();
     \Amp\all($lengths)->when(function ($error, $result) use($promisor) {
         if ($error) {
             $promisor->fail($error);
         } else {
             $promisor->succeed(array_sum($result));
         }
     });
     return $promisor->promise();
 }
Пример #4
0
<?php

require './example_bootstrap.php';
\Amp\run(function () {
    $db = new \Amp\Mysql\Pool("host=" . DB_HOST . ";user="******";pass="******";db=" . DB_NAME);
    /* Create table and insert a few rows */
    /* we need to wait until table is finished, so that we can insert. */
    (yield $db->query("CREATE TABLE IF NOT EXISTS tmp SELECT 1 AS a, 2 AS b"));
    $promises = [];
    foreach (range(1, 5) as $num) {
        $promises[] = $db->query("INSERT INTO tmp (a, b) VALUES ({$num}, {$num} * 2)");
    }
    /* wait until everything is inserted (in case where we wouldn't have to wait, we also could just  */
    (yield \Amp\all($promises));
    print "Insertion successful (if it wasn't, an exception would have been thrown by now)\n";
    $db->close();
});
Пример #5
0
<?php

function work()
{
    $time_start = microtime(true);
    $np = 0;
    for ($i = 0; $i <= rand(0, 1000); $i++) {
        $x = lcg_value() * 2 - 1;
        $y = lcg_value() * 2 - 1;
        if ($x ** 2 + $y ** 2 <= 1) {
            $np++;
        }
    }
    return json_encode(array('duration' => microtime(true) - $time_start, 'results' => $np, 'start_time' => $this->time_start));
}
$dispatcher = new Amp\Thread\Dispatcher();
// call 2 functions to be executed asynchronously
$promise1 = $dispatcher->call('work');
$promise2 = $dispatcher->call('work');
$promise3 = $dispatcher->call('work');
$promise4 = $dispatcher->call('work');
$comboPromise = Amp\all([$promise1, $promise2, $promise3, $promise4]);
list($result1, $result2) = $comboPromise->wait();
Пример #6
0
 /**
  * @param array $keys
  * @return \Amp\Promise
  */
 public function deleteMulti(array $keys)
 {
     $promises = [];
     foreach ($keys as $key) {
         $promises[] = $this->send(['delete', $key]);
     }
     return \Amp\all($promises);
 }
Пример #7
0
<?php

require __DIR__ . '/../vendor/autoload.php';
/**
 * Note that Client::requestMulti() is nothing more than a convenience wrapper
 * to prevent us from having to call Client::request() several times and store
 * the resulting promises in an array ourselves. Doing so would have the exact
 * same effect and all requests would be executed in parallel either way.
 */
$promises = (new Amp\Artax\Client())->requestMulti(['google' => 'http://www.google.com', 'news' => 'http://news.google.com', 'bing' => 'http://www.bing.com', 'yahoo' => 'https://www.yahoo.com']);
// Tolerate errors in some of the requests. If any one of the promises in our array
// succeeds then the result is a two-item array of errors and successes. If *none*
// of our response promises succeed then this line will throw.
$comboPromise = Amp\some($promises);
list($errors, $responses) = Amp\wait($comboPromise);
// Alternatively we could use the following line to require all of our responses to succeed. If
// any one of the response promises resolves as a failure then this line will throw:
$comboPromise = Amp\all($promises);
$responses = Amp\wait($comboPromise);
// Now, let's iterate over the responses to demonstrate that they retain the same keys from
// our original call to Amp\Artax\Client::request():
foreach ($responses as $key => $response) {
    printf("%s | HTTP/%s %d %s\n", $key, $response->getProtocol(), $response->getStatus(), $response->getReason());
}