Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 脚本/队列内存不足_Php_Laravel 5_Redis_Push Queue - Fatal编程技术网

Php 脚本/队列内存不足

Php 脚本/队列内存不足,php,laravel-5,redis,push-queue,Php,Laravel 5,Redis,Push Queue,每当用户请求时,我的应用程序都需要处理大量数据。该脚本最初是在foreach循环中组织的,但这导致PHP每次都超时。我开始使用Redis队列,但后来我遇到了内存问题 mmap() failed: [12] Cannot allocate memory 及 现在我已经将队列设置为只有一个进程。它工作得更好,但过了一段时间,我又开始出现内存错误。这只是我在测试它。一旦用户开始使用它,它就会崩溃 我分配脚本1024MB,因为如果不一次性使用,它的内存就会耗尽。我想知道在每次运行脚本之后是否可以做些什

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

mmap() failed: [12] Cannot allocate memory

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

我分配脚本1024MB,因为如果不一次性使用,它的内存就会耗尽。我想知道在每次运行脚本之后是否可以做些什么来释放内存。像不稳定的变量?不过,我看不出这有什么帮助,因为脚本已经结束,队列工作人员将从头开始重新运行

我使用的是一台有2GB内存的流浪机器(宅地)

更新:

当我们执行dispatcher时,回溯测试就开始了,并运行了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_解码会加快速度


我还删除了脚本末尾的db insert,但我更希望它留在脚本中,因为这会让我的生活更轻松。

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


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

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


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

当PHP脚本结束时,内存会自动释放。这里显示的是128MB的限制(134217728字节),而不是1024MB。如果不提供任何代码,我不知道我们能提供什么帮助。@Devon-谢谢。我添加了一些代码。当PHP脚本结束时,内存会自动释放。这里显示的是128MB的限制(134217728字节),而不是1024MB。如果不提供任何代码,我不知道我们能提供什么帮助。@Devon-谢谢。我添加了一些代码。
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));