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时,不存在进程隔离,可以使用全局变量保存数据
##
$fds = array(); $server->on('connect', function ($server, $fd){ echo "connection open: {$fd}\n"; global $fds; $fds[] = $fd; var_dump($fds); });
The corresponding solution is to use external storage services:
数据库,如:MySQL、MongoDB 缓存服务器,如:Redis、Memcache 磁盘文件,多进程并发读写时需要加锁
Ordinary database and disk file operations have a lot of IO waiting time. Therefore it is recommended to use:
Redis 内存数据库,读写速度非常快 /dev/shm 内存文件系统,读写操作全部在内存中完成,无IO消耗,性能极高 除了使用存储之外,还可以使用共享内存来保存数据
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!