process是swoole进程管理类。
使用process会自动使用异步处理。
默认开启协程的情况下不允许创建进程。
https://wiki.swoole.com/zh-cn/#/process/process
$workers = [];$process = new Process(function(){echo'Child #' . getmypid() . " start and sleep 3s" . PHP_EOL; sleep(3);echo'Child #' . getmypid() . ' exit' . PHP_EOL;});$pid = $process->start();$workers[$pid] = $process;$process = new Process(function(){echo'Child #' . getmypid() . " start and sleep 1s" . PHP_EOL; sleep(1);echo'Child #' . getmypid() . ' exit' . PHP_EOL;});$pid = $process->start();$workers[$pid] = $process;$process = new Process(function(){echo'Child #' . getmypid() . " start and sleep 4s" . PHP_EOL; sleep(4);echo'Child #' . getmypid() . ' exit' . PHP_EOL;});$pid = $process->start();$workers[$pid] = $process;for ($n = count($workers); $n--;) { $status = Process::wait(true);echo"Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}" . PHP_EOL;}echo'Parent #' . getmypid() . ' exit' . PHP_EOL;输出结果
root@2ee7183b3f43:/wj/test/swoole/process# php test2.phpChild #16756 start and sleep 3sChild #16757 start and sleep 1sChild #16758 start and sleep 4sChild #16757 exitRecycled #16757, code=0, signal=0Child #16756 exitRecycled #16756, code=0, signal=0Child #16758 exitRecycled #16758, code=0, signal=0Parent #16755 exit睡眠1秒的进程16757先结束,睡眠3秒的进程16756再结束,睡眠4秒的进程16758最后结束,证明三个进程都是异步。
$workers = [];$process = new Process(function(){echo'Child #' . getmypid() . " start and sleep 3s time:" . date("Y-m-d H:i:s") . PHP_EOL; sleep(3);echo'Child #' . getmypid() . ' exit time:' . date("Y-m-d H:i:s") . PHP_EOL;});$pid = $process->start();$workers[$pid] = $process;$process = new Process(function(){echo'Child #' . getmypid() . " start and sleep 1s time:" . date("Y-m-d H:i:s") . PHP_EOL; sleep(1);echo'Child #' . getmypid() . ' exit time:' . date("Y-m-d H:i:s") . PHP_EOL;});$pid = $process->start();$workers[$pid] = $process;for ($n = count($workers); $n--;) { $status = Process::wait(true); $pid = $status['pid']; $process = $workers[$pid];echo"Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']} time:" . date("Y-m-d H:i:s") . PHP_EOL; $msg = $process->read();echo"process msg:" . $msg . " time:" . date("Y-m-d H:i:s") . PHP_EOL;}echo'Parent #' . getmypid() . ' exit' . PHP_EOL;Process构造默认第二参数为false,代表Process执行过程中输出内容打印到屏幕。
但是再之后又用read()获取返回内容,这样是错误的用法而且影响比较大。
运行结果
Child #23 start and sleep 3s time:2026-01-24 01:44:09Child #24 start and sleep 1s time:2026-01-24 01:44:09Child #24 exit time:2026-01-24 01:44:10Recycled #24, code=0, signal=0 time:2026-01-24 01:44:10Child #23 exit time:2026-01-24 01:44:12process msg: time:2026-01-24 01:45:10Recycled #23, code=0, signal=0 time:2026-01-24 01:45:10process msg: time:2026-01-24 01:46:10Parent #22 exit每次输出“process msg:”都和上一次执行相隔一分钟,明显非常耽误执行速度。
将第二个参数改为true,代表输出内容写入到主进程管道,或者不用read(),执行都会正常。
第二参数改为true 运行结果
Recycled #27, code=0, signal=0 time:2026-01-24 01:58:03process msg:Child #27 start and sleep 1s time:2026-01-24 01:58:02Child #27 exit time:2026-01-24 01:58:03 time:2026-01-24 01:58:03Recycled #26, code=0, signal=0 time:2026-01-24 01:58:05process msg:Child #26 start and sleep 3s time:2026-01-24 01:58:02Child #26 exit time:2026-01-24 01:58:05 time:2026-01-24 01:58:05Parent #25 exit在tcp/udp/http/websocket中都可以创建进程,但有些回调会自动开启协程,协程中不能手动创建进程。
在server设置enable_coroutine配置,设置是否开启协程。
受enable_coroutine影响的回调:
往期回顾





扫描识别二维码关注我们获取更多