我在 Symfony 5.3 中的專案中使用帶有 RabittMQ 服務器的Messenger 組件。我想管理我的 MessageHandler 記憶體,因為我的代碼占用了太多記憶體 ( Fatal error: Allowed memory size of 2147483648 bytes exhausted (tried to allocate 33554440 bytes)。
對于每條消費的訊息,我感覺就像MessageHandler保留了前一條的記憶MessageHandler。這是我運行命令的課程:
class MessageHandler implements MessageHandlerInterface
{
private KernelInterface $kernel;
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
}
/**
* @param RequestMessage $requestMessage
* @throws \Exception
*/
public function __invoke(RequestMessage $requestMessage)
{
$application = new Application($this->kernel);
$application->setAutoExit(false);
$input = new ArrayInput([
'command' => 'app:my-command',
'userId' => $requestMessage->getUserId(),
'--no-debug' => ''
]);
$output = new BufferedOutput();
$application->run($input, $output);
}
}
我用這個命令消費我的訊息:
$ php bin/console messenger:consume -vv
我正在尋找一種解決方案來使用獨立記憶體來使用我的每條訊息。我不知道問題出在哪里,如果有人可以幫助我。
我可以想到記憶體泄漏,但我不明白為什么沒有清理訊息的記憶體。
uj5u.com熱心網友回復:
部署到生產
在生產中,有一些重要的事情需要考慮:
不要讓工人永遠運行
一些服務(如 Doctrine 的 EntityManager)會隨著時間的推移消耗更多的記憶體。因此,不要讓您的作業人員永遠運行,而是使用像 messenger:consume --limit=10 這樣的標志來告訴您的作業人員在退出之前只處理 10 條訊息(然后主管將創建一個新行程)。還有其他選項,例如 --memory-limit=128M 和 --time-limit=3600。
來自:Symfony Messenger Docs
uj5u.com熱心網友回復:
首先,我知道我有記憶體問題,但我必須找到解決方案。
為了運行我的命令,我使用 Symfony 的Process組件在不同的行程中運行我的命令,以免 RAM 過載并獨立運行。這是我的決議代碼:
use Symfony\Component\Process\Process;
$completedCommand = 'php bin/console app:my-command ' . $user->getId() . ' --no-debug';
$process = Process::fromShellCommandline($completedCommand);
$process->run();
if (!$process->isSuccessful()) {
// Example: Throw an exception...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/417804.html
標籤:
