You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Amphp Parallel在PHP-FPM临时目录生成大量sock文件,是否正常?如何清理?

amp-parallel 遗留socket文件问题解答

你的代码示例:

$executions = [];
foreach ($urls as $url) {
    // FetchTask is just an example, you'll have to implement
    // the Task interface for your task.
    $executions[$url] = Worker\submit(new FetchTask($url));
}

// Each submission returns an Execution instance to allow two-way
// communication with a task. Here we're only interested in the
// task result, so we use the Future from Execution::getFuture()
$responses = Future\await(array_map(
    fn (Worker\Execution $e) => $e->getFuture(),
    $executions,
));

这不是预期行为

正常情况下,amp-parallel-*.sock这类socket文件会在特定时机自动清理:

  • 对应的Worker进程正常退出时,socket文件会被系统自动回收。
  • 当Execution实例的引用计数归0(被PHP垃圾回收机制销毁)时,库的内部清理逻辑会删除socket文件。

未清理的常见原因

  1. PHP-FPM进程常驻特性:PHP-FPM的worker进程生命周期长,Execution实例可能被全局变量、闭包捕获等原因占用,无法被垃圾回收,导致资源泄漏。
  2. 异常中断:任务执行过程中触发未捕获的异常,导致清理代码未被执行。
  3. 版本bug:旧版本的amp-parallel库存在资源泄漏问题,未正确处理socket文件的清理逻辑。

解决办法

  1. 显式清理资源:在任务完成后,主动销毁Execution实例触发清理:
// 获取结果后遍历执行实例,主动取消并释放资源
foreach ($executions as $execution) {
    $execution->cancel();
    unset($execution);
}
unset($executions);
// 可选:手动触发垃圾回收,加速资源释放
gc_collect_cycles();
  1. 定时清理过期文件:通过系统定时任务(如Linux cron)定期清理临时目录中超过阈值的socket文件,比如删除24小时前的文件:
find /path/to/php-fpm/tmp -name "amp-parallel-*.sock" -mtime +1 -delete
  1. 升级库版本:将amp-parallel库升级到最新稳定版,修复已知的资源泄漏bug。

内容的提问来源于stack exchange,提问作者MindGamer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 19:10:19