如何获取通过 symfony 进程运行的脚本的内存使用情况?

How to get the memory usage of a script that runs via symfony-process?

所以,这是这个 wep 应用程序,愉快地 运行 python 脚本,通过消息排队,使用 Symfony Process 组件:

$stopwatch = new Stopwatch();
$stopwatch->start('cmd');

$process = new Process($somePythonCmd, null, ['PYENV_VERSION' => 'unicornenv']);
$process->start();
$process->wait();

$event = $stopwatch->stop('cmd');
$metrics = [
    'duration' => Helper::formatTime($event->getDuration() / 1000),
    'memory' => Helper::formatMemory($event->getMemory())
];

我认为这是衡量 PHP 进程本身的内存消耗,对吗?

有没有办法测量python脚本的实际内存使用情况?

一些解决方法:

...但是您可以在命令前加上 /usr/bin/time,
这将为您提供流程 errorOutput

中的指标
$cmd = [
            '/usr/bin/time',
            'python',
            'script',
        ];

$process = new Process($cmd, null, ['PYENV_VERSION' => 'someenv']);
$process->start();
$process->wait();

if ($process->isSuccessful()) {
    $metrics = $process->getErrorOutput();
    //14.60user 0.65system 0:09.54elapsed 159%CPU (0avgtext+0avgdata 1292616maxresident)k 24inputs+72outputs (0major+346624minor)pagefaults 0swaps
}