例如,下面的代码
<?php
if (some_condition()) {
header('Location: /');
exit();
}
// do a lot of things...在经典的PHP FPM上,前面的代码运行得很好。但在PHP+Swoole上,我们会出现以下错误
swoole exit {"exception":"[object] (Swoole\\ExitException(code: 0): swoole exit at这个错误是可以理解的。但是,迁移它的最简单方法是什么?
发布于 2021-07-04 07:56:23
对于PHP FPM,每段代码都是单独执行的,结果(该进程的输出)通过管道返回给客户机。因此,每当我们想要停止处理时,我们都会触发exit()。
但是,使用Swoole时,服务器会一直运行。当然,也可以使用Swoole\ stop the Process::stop the Process()退出--但这通常取决于控制器是否触发并立即发送响应。例如:
$server = new Swoole\HTTP\Server("127.0.0.1", 9501);
$server->on("start", function (Swoole\Http\Server $server) {
echo "Swoole http server is started at http://127.0.0.1:9501\n";
});
$server->on("request", function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
$response->header("Content-Type", "text/plain");
$response->end("Hello World\n");
});
$server->start();在本例中,$response->end方法在本质上与PHP FPM中的exit()做同样的事情。请注意,这与Node世界中发生的事情非常相似:服务器应该一直在运行,由控制器(处理每个单独的请求的函数)决定是否停止处理单独的请求并传回响应,包括头和主体。
发布于 2021-07-06 09:51:01
您可以了解\Swoole\ExitException。
use Swoole\Coroutine;
use function Swoole\Coroutine\run;
function route()
{
controller();
}
function controller()
{
your_code();
}
function your_code()
{
Coroutine::sleep(.001);
exit(1);
}
run(function () {
try {
route();
} catch (\Swoole\ExitException $e) {
var_dump($e->getMessage());
var_dump($e->getStatus() === 1);
var_dump($e->getFlags() === SWOOLE_EXIT_IN_COROUTINE);
}
});发布于 2021-07-05 03:45:23
如何在Laravel + Swoole上替换exit()
是的,这是一个非常糟糕的exit()函数。但自己的Laravel应用引导遗留代码。然后,根据相关的成本,这是一个更好的解决方案。
class ExitException extends Exception implements Renderable
{
public function report() {
return true; // dont report
}
public function render()
{
return ' '; // prevent 500 html dump error
}
}exit();替换为// exit;
throw new ExitException();该异常将由您的应用错误处理程序处理,并向Swoole请求处理程序发送一个空字符串作为响应。
我再说一遍:这不是一个优雅的解决方案,只是防止在遗留应用程序上更改大量代码。
https://stackoverflow.com/questions/68240709
复制相似问题