Пример #1
0
 /**
  * @codeCoverageIgnore
  */
 protected function processWork() : \Generator
 {
     $transmitter = new SocketTransmitter(new SocketStream($this->socket));
     while (true) {
         list($type, $payload) = (yield from $transmitter->receive());
         if ($type === SocketTransmitter::TYPE_EXIT) {
             yield from $transmitter->send($payload, $type);
             break;
         }
         try {
             if (isset($payload['func'])) {
                 if (\substr($payload['func'], 0, 1) === '@') {
                     $result = \substr($payload['func'], 1)(...$payload['args'] ?? []);
                 } else {
                     $result = $payload['func'](...$payload['args'] ?? []);
                 }
             } elseif (isset($payload['class']) && isset($payload['method'])) {
                 $result = $payload['class']::{$payload['method']}(...$payload['args'] ?? []);
             } else {
                 throw new \RuntimeException('No callable target passed to threaded worker');
             }
         } catch (\Throwable $e) {
             yield from $transmitter->sendError($e);
             continue;
         }
         yield from $transmitter->send($result, SocketTransmitter::TYPE_DATA);
     }
 }
Пример #2
0
                list($type, $payload) = (yield from $transmitter->receive());
                if ($type === SocketTransmitter::TYPE_EXIT) {
                    yield from $transmitter->send($payload, $type);
                    $socket->close();
                    $exitCode = 0;
                    break;
                }
                try {
                    if (isset($payload['func'])) {
                        if (\substr($payload['func'], 0, 1) === '@') {
                            $result = \substr($payload['func'], 1)(...$payload['args'] ?? []);
                        } else {
                            $result = $payload['func'](...$payload['args'] ?? []);
                        }
                    } elseif (isset($payload['class']) && isset($payload['method'])) {
                        $result = $payload['class']::{$payload['method']}(...$payload['args'] ?? []);
                    } else {
                        throw new \RuntimeException('No callable target passed to threaded worker');
                    }
                } catch (\Throwable $e) {
                    yield from $transmitter->sendError($e);
                    continue;
                }
                yield from $transmitter->send($result, SocketTransmitter::TYPE_DATA);
            }
        } finally {
            Loop::stop();
        }
    });
}, new NativeLoop());
exit($exitCode);