首页
仓库
文档
nginx手册
Docker手册
workerman
Flask
PHP
python
RabbitMQ
其他
Linux
占位1
占位2
目录
多进程之间是不能跨进程互相操作对方空间的变量的,可以考虑使用官方的 globalData组件 或者 redis组件来实现 composer require workerman/globaldata ###1 先开启服务端 <?php require 'vendor/autoload.php'; //自动加载 use Workerman\Worker; $global_worker = new GlobalData\Server('127.0.0.1', 2100);//创建本地数据共享 Worker::runAll(); ?> ###2 在进程之间 调用 可以在运行各种进程之前 初始化内容 $worker = new Worker("tcp://0.0.0.0:18900"); $worker->name = 'testWorker'; $worker->count = 8; $global = new GlobalData\Client('127.0.0.1:2100'); $global->count = 0; //链接人数 $global->data = [];//具体数据 ** $worker->onWorkerStart $worker->onConnect 如何使用: $worker->onConnect = function($connection) use($worker) { global $global; $global = new \GlobalData\Client('127.0.0.1:2100'); $global->increment('count'); //增加 $odata=$newdata= $global->data; $newdata[$worker->id][$connection->id]=$connection->getRemoteIp(); $global->cas('data', $odata, $newdata); //替换 老数据 新数据 (同时并发 数据丢失) echo '当前人数:'.$global->count."\n"; var_dump($global->data); echo "\n"; echo "new connection from worker:".$worker->id.' conn:' . $connection->id.' ip:'. $connection->getRemoteIp() . "\n"; file_put_contents('onConnect.txt',"new connection from worker:".$worker->id.'conn:' . $connection->id.' ip:'. $connection->getRemoteIp()."\r\n",FILE_APPEND); };