You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Issue:
I ran a simple http server script using swoole 6 and SWOOLE_THREAD mode, the prinft show many times in console but it only shows 1 time in SWOOLE_PROCESS mode. It seems like this script run 1 time for each worker thread. Is this an expected behaviour or not?
1. What did you do? If possible, provide a simple script for reproducing the error.
I ran this script with Swoole 6
<?phprequire_once__DIR__ . '/../vendor/autoload.php';
useSwoole\Constant;
useSwoole\Http\Request;
useSwoole\Http\Response;
useSwoole\Http\Server;
useWorkerman\Events\Swoole;
$server = newServer('0.0.0.0', 3001, SWOOLE_THREAD);
$server->set([
'hook_flags' => SWOOLE_HOOK_ALL,
'enable_coroutine' => true,
'task_enable_coroutine' => true,
'debug_mode' => true,
'display_errors' => true,
'enable_preemptive_scheduler' => true,
'worker_num' => 4,
'reactor_num' => 2,
'task_worker_num' => 2
]);
$server->on(Constant::EVENT_WORKER_START, function () {
$pid = getmypid();
printf("Worker start on $pid\n");
});
$server->on('Task', function ($server, $task) {});
$server->on('request', function (Request$request, Response$response) {
$response->end("OK");
});
$pid = getmypid();
printf("[thread] Server started at http://0.0.0.0:$server->port on process {$pid}\n");
$server->start();
2. What did you expect to see?
Line _[thread] Server started at _ shows only 1 time.
3. What did you see instead?
Line _[thread] Server started at _ shows only 8 times.
4. What version of Swoole are you using (show your php --ri swoole)?
swoole
Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 6.0.0
Built => Dec 25 2024 10:41:50
coroutine => enabled with boost asm context
epoll => enabled
eventfd => enabled
thread => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 3.0.2 15 Mar 2022
dtls => enabled
http2 => enabled
json => enabled
pcre => enabled
zlib => 1.2.11
brotli => E16777225/D16777225
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
Directive => Local Value => Master Value
swoole.enable_library => On => On
swoole.enable_fiber_mock => Off => Off
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608
5. What is your machine environment used (show your uname -a & php -v & gcc -v) ?
uname -a
Linux PF4GRXXV 6.8.0-49-generic #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
php -v
PHP 8.4.2 (cli) (built: Dec 25 2024 09:55:31) (ZTS)
Copyright (c) The PHP Group
Zend Engine v4.4.2, Copyright (c) Zend Technologies
with Zend OPcache v8.4.2, Copyright (c), by Zend Technologies
This is as expected. Unlike processes, multithreading in PHP operates differently because of the thorough isolation enforced by PHP's ZTS (Zend Thread Safety). As a result, child threads cannot inherit any resources from the parent thread, meaning each worker thread runs in a brand new VM environment.
The code segment containing new Server in a child thread will be re-executed within its own context.
There are two key configurations related to SWOOLE_THREAD:
The bootstrap option allows you to set an entry file for worker threads; by default, it uses the index.php file specified when running php-cli.
The init_arguments option enables the creation of resources that can be passed as parameters to worker threads. These thread parameters can be accessed within worker threads using the Thread::getArguments() method.
Issue:
I ran a simple http server script using swoole 6 and SWOOLE_THREAD mode, the prinft show many times in console but it only shows 1 time in SWOOLE_PROCESS mode. It seems like this script run 1 time for each worker thread. Is this an expected behaviour or not?
1. What did you do? If possible, provide a simple script for reproducing the error.
I ran this script with Swoole 6
2. What did you expect to see?
Line _[thread] Server started at _ shows only 1 time.
3. What did you see instead?
Line _[thread] Server started at _ shows only 8 times.
4. What version of Swoole are you using (show your php --ri swoole)?
5. What is your machine environment used (show your
uname -a
&php -v
&gcc -v
) ?uname -a
Linux PF4GRXXV 6.8.0-49-generic #49~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Nov 6 17:42:15 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
php -v
gcc -v
The text was updated successfully, but these errors were encountered: