Skip to content

[QUESTION] Constant::OPTION_WORKER_NUM 这个参数是跟swoole里面的一个作用么?为什么我设置了以后 无效呢。 #5098

Answered by her-cat
lixinhan asked this question in Q&A
Discussion options

You must be logged in to vote

为什么「纯 Swoole 代码」与「Hyperf 代码」的预期效果不一样?

根本原因是两者的配置项不一样

「纯 Swoole 代码」的 HTTP 服务仅设置了 worker_num 和 daemonize 两个配置项。
而 Hyperf 除此之外还设置了hook_flags 配置项,默认值为 SWOOLE_HOOK_ALL,表示 hook 所有会发生阻塞的原生 PHP 函数,其中就包括了 sleep 函数。

所以,只需要在「纯 Swoole 代码」的配置项中加入 hook_flags,并设置值为 SWOOLE_HOOK_ALLSWOOLE_HOOK_SLEEP,就能得到跟「Hyperf 代码」一样的效果了。

<?php
$http = new Swoole\Http\Server('127.0.0.1', 9503);
//设置worker进程4个
$http->set([
    'worker_num' => 1,
    'daemonize' => false,
    'hook_flags' => SWOOLE_HOOK_SLEEP, // 新增的配置项
]);

$http->on('start', function ($server) {
    echo "Swoole http server is started at http://127.0.0.1:9503\n";
});

$http->on('request', function ($request, $response) {
    $response->header('Conten…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by limingxinleo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested
2 participants
Converted from issue

This discussion was converted from issue #5094 on September 21, 2022 08:13.