public function next() { $coroutine = $this->_coroutine; if ($coroutine->valid()) { \Coroutine::resume($coroutine); } }
protected static function _wrap($coroutine) { $taskId = self::newTaskId(); $coroutine = (function ($taskId, $coroutine) { $resp = (yield from $coroutine); \Coroutine::unregister($taskId); })($taskId, $coroutine); self::register($taskId, $coroutine); return $coroutine; }
public function multiCoroutine($taskId, $coroutine) { try { $resp = (yield from $coroutine); $this->_callRsp[] = $resp; if (count($this->_callRsp) == count($this->_callList)) { $this->executeCoroutine($this->_callRsp); $this->next(); } \Coroutine::unregister($taskId); } catch (\Exception $e) { $this->executeCoroutine(null, $e); $this->next(); } }
/** * @return Generator * @throws \Steelbot\TelegramBotApi\Exception\TelegramBotApiException */ function botCoroutine() : \Generator { $api = new Api(getenv('BOT_TOKEN')); $updateId = 1; printf("Waiting for updates ...\n"); while (true) { // waiting for updates from telegram server /** @var Update[] $updates */ $updates = (yield from $api->getUpdates($updateId)); foreach ($updates as $update) { $method = processUpdate($update); if (is_object($method)) { yield from $api->execute($method); } $updateId = $update->updateId; } } } $coroutine = new Coroutine(botCoroutine()); $coroutine->done(null, function (\Throwable $exception) { echo "Exception catched:\n"; echo " Code: {$exception->getCode()}\n"; echo " Message: {$exception->getMessage()}\n"; echo " File: {$exception->getFile()}\n"; echo " Line: {$exception->getLine()}\n"; }); Loop\run();
public function next() { if ($this->_coroutine->valid()) { $coValue = $this->_coroutine->current(); if (!$coValue instanceof \Client\Tcp) { \Coroutine::resume($this->_coroutine); $this->sleep(); //当前协程不再监听读事件,则移除读事件 } } }
public function next() { if ($this->_coroutine->valid()) { $coValue = $this->_coroutine->current(); if (!$coValue instanceof \Client\Websocket) { \Coroutine::resume($this->_coroutine); $this->sleep(); //当前协程不再监听读事件,则移除读事件 return false; } else { return true; } } }
public static function release($client) { \Pool::release(static::TASK_QUEUE, $client); \Coroutine::next(static::TASK_QUEUE); }