Home > PHP Framework > Swoole > Is swoole multi-process or multi-threaded?

Is swoole multi-process or multi-threaded?

(*-*)浩
Release: 2019-12-06 14:23:29
Original
4203 people have browsed it

Is swoole multi-process or multi-threaded?

Since the PHP language does not support multi-threading, Swoole uses multi-process mode. There is process memory isolation in multi-process mode. When global variables and super-global variables are modified in the working process, it will be invalid in other processes. (Recommended Learning: SWOOLE Video Tutorial )

设置worker_num=1时,不存在进程隔离,可以使用全局变量保存数据
Copy after login

##

$fds = array();
$server->on('connect', function ($server, $fd){
    echo "connection open: {$fd}\n";
    global $fds;
    $fds[] = $fd;
    var_dump($fds);
});
Copy after login

$ FDS Although it is a global variable, it is only currently Valid within the process. The bottom layer of the Swoole server will create multiple Worker processes. The value printed in var_dump($fds) only contains partial connected fds.

The corresponding solution is to use external storage services:

数据库,如:MySQL、MongoDB
缓存服务器,如:Redis、Memcache
磁盘文件,多进程并发读写时需要加锁
Copy after login

Ordinary database and disk file operations have a lot of IO waiting time. Therefore it is recommended to use:

Redis 内存数据库,读写速度非常快
/dev/shm 内存文件系统,读写操作全部在内存中完成,无IO消耗,性能极高
除了使用存储之外,还可以使用共享内存来保存数据
Copy after login

The above is the detailed content of Is swoole multi-process or multi-threaded?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template