protected function wait($interval, $callback) { $future = new Future(); swoole_timer_after($interval * 1000, function () use($future, $callback) { Future\sync($callback)->fill($future); }); return $future; }
<?php require_once "../vendor/autoload.php"; use Hprose\Future; $p1 = new Future(function () { return array(Future\value(1), Future\value(2)); }); $p1->then(function ($value) { var_dump($value); }); $p2 = Future\sync(function () { return array(Future\value(1), Future\value(2)); }); $p2->then(function ($value) { var_dump($value); });
function sync($computation) { return Future\sync($computation); }
public function loop() { $self = $this; $multicurl = curl_multi_init(); while (($count = count($this->curls)) > 0) { $curls = $this->curls; $this->curls = array(); $results = $this->results; $this->results = array(); foreach ($curls as $curl) { curl_multi_add_handle($multicurl, $curl); } $err = null; try { $active = null; $status = $this->curlMultiExec($multicurl, $active); while ($status === CURLM_OK && $count > 0) { $status = $this->curlMultiExec($multicurl, $active); $msgs_in_queue = null; while ($info = curl_multi_info_read($multicurl, $msgs_in_queue)) { $handle = $info['handle']; $index = array_search($handle, $curls, true); $results[$index]->resolve(Future\sync(function () use($self, $info, $handle) { if ($info['result'] === CURLM_OK) { return $self->getContents(curl_multi_getcontent($handle)); } throw new Exception($info['result'] . ": " . curl_error($handle)); })); --$count; if ($msgs_in_queue === 0) { break; } } } } catch (Exception $e) { $err = $e; } catch (Throwable $e) { $err = $e; } foreach ($curls as $index => $curl) { curl_multi_remove_handle($multicurl, $curl); curl_close($curl); if ($err !== null) { $results[$index]->reject($err); } } } curl_multi_close($multicurl); }