代码之家  ›  专栏  ›  技术社区  ›  Sujit Baniya

如何向laravel队列作业发送迭代器

  •  1
  • Sujit Baniya  · 技术社区  · 6 年前

    当尝试向Laravel作业(Horizon和Redis)发送10万条记录的数组负载时,会引发内存问题:

    Allowed memory size of 536870912 bytes exhausted (tried to allocate 77598720 bytes)
    

    我用的代码是

    CreateSmsBroadcast::dispatch($user, $smsBroadcast, iterator_to_array($contacts),
     $requestData)->onQueue('create-sms-broadcast');
    

    通过有效载荷时 contacts (iterator) ,

    CreateSmsBroadcast::dispatch($user, $smsBroadcast, $contacts, $requestData)
    ->onQueue('create-sms-broadcast');
    

    然后出现以下问题:

        // vendor/laravel/framework/src/Illuminate/Queue/Queue.php
        protected function createObjectPayload($job)
        {
            return [
                'displayName' => $this->getDisplayName($job),
                'job' => 'Illuminate\Queue\CallQueuedHandler@call',
                'maxTries' => $job->tries ?? null,
                'timeout' => $job->timeout ?? null,
                'timeoutAt' => $this->getJobExpiration($job),
                'data' => [
                    'commandName' => get_class($job),
                    'command' => serialize(clone $job), // line of error
                ],
            ];
        }
    
    Serialization of 'Generator' is not allowed
    

    我不想增加记忆。所以请建议我如何解决上述问题?

    0 回复  |  直到 6 年前
    推荐文章