gpt4 book ai didi

php - 脚本/队列不断耗尽内存

转载 作者:可可西里 更新时间:2023-11-01 10:54:26 25 4
gpt4 key购买 nike

每当用户请求时,我的应用程序都需要处理大量数据。该脚本最初组织在一个 foreach 循环中,但这导致 PHP 每次都超时。我转而使用 Redis 队列,但后来我遇到了内存问题。

mmap() failed: [12] Cannot allocate memory

PHP Fatal error:  Out of memory (allocated 79691776) (tried to allocate 134217728 bytes) 

现在我已将队列设置为只有一个进程。它工作得更好,但过了一会儿我又开始出现内存错误。这只是我在测试它。一旦用户开始使用它,它就会倒下。

我为脚本分配了 1024MB,因为如果我不一次性使用它,它就会用完内存。我想知道每次运行脚本以释放内存后我是否可以做些什么。喜欢取消设置变量?不过,我看不出这会有什么帮助,因为脚本结束并由队列工作人员从头开始再次运行。

我正在使用具有 2GB RAM 的流浪机器 (Homestead)

更新:

回测从执行调度器开始,历时10个联盟,10年。

调度程序类:

class Dispatcher
{
use Attributes;
use DataRetriever;
public function runBacktest($token)
{
$authUserId = Auth::user()->id;
$system = System::where('token', $token)->first();
$this->getSystem( $authUserId, $system->id);
foreach ($this->leagues as $league) {
foreach ($this->years as $year) {
BacktestJob::dispatch($authUserId, $system->id, $token, $league, $year);
}
}
}
}

调度员执行作业:

class BacktestJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

private $token;
private $league;
private $year;
private $authUserId;
private $systemId;

public $timeout = 10000;


/**
* Create a new job instance.
*
* @return void
*/
public function __construct($authUserId, $systemId, $token, $league, $year)
{
$this->token = $token;
$this->league = $league;
$this->year = $year;
$this->authUserId = $authUserId;
$this->systemId = $systemId;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$backtest = new Backtest;
$backtest->init($this->authUserId, $this->systemId, $this->token, $this->league, $this->year);
}
}

下面是主脚本的精简版,因为它做了很多事情:

public function init($authUserId, $systemId, $token, $league, $year)
{
// ini_set('memory_limit', -1);
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
ini_set('memory_limit', '1024M'); // or you could use 1G
$this->authUserId = $authUserId;
$this->systemId = $systemId;
$this->token = $token;
include(storage_path("app/matches/{$league->key}/{$year}.php"));
$this->leagueResults[$league->key][$year] = collect($this->leagueResults[$league->key][$year]);
//Loops through the data - saves to new array
fwrite($file, serialize($backtest));

最初数据是从一个 50MB 的 json 文件中提取的。我用硬编码的 PHP 数组(文件大小 100MB)替换了 json 文件。我知道较新的文件更大,但我假设不通过 json_decode 会加快速度。

我还在脚本末尾删除了一个数据库插入,但我更希望它留在原处,因为它会让我的生活更轻松。

最佳答案

好的,我通过分解数据文件解决了这个问题。因为我可以完全访问原始数据文件,而且我不需要每个请求都包含其中的所有数据,所以我将其分成大约 50 个较小的文件。

我对提高的性能感到惊讶。从 30 多秒加上超时,它下降到不到 2 秒。大多数请求在不到一秒内完成。

关于php - 脚本/队列不断耗尽内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46893449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com