如何在Laravel中执行多个后台进程? [英] How to execute more than one background process in laravel?

查看:74
本文介绍了如何在Laravel中执行多个后台进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我了解队列,现在对队列有很好的经验.队列的问题是,它是一个队列.我想在后台一起执行多个功能或命令.队列将把第二个命令或函数保留在队列中,并在第一个命令或函数执行完毕后立即执行!

First of all, I know about queues and now have good experience with queues. The problem with the queue is, it is a queue. I want to execute multiple functions or commands together in the background. Queues will keep second command or function in a queue and will execute once the first one is done executing!

例如,我有一个包含3,000,000条记录的表,我想更快地处理它们.我所能做的就是将它们分成5个相等的块,并总共执行5个命令,这样我就可以利用我的CPU并以5倍的速度处理数据.

For example, I have a table with ~3,000,000 records and I want to process them faster. What I can do is divide them into 5 equal chunks and execute 5 commands altogether so that I can utilize my CPU as well as process data 5 times faster.

那么,我该如何使用Laravel做到这一点?队列无法正常工作,因为它们一个接一个地执行任务.如果您的想法是创建5个以上的多个队列和主管来完成任务,那么我认为这不是标准的方法.

So, How can I do this with Laravel? Queues are not going to work because they execute stuff one after another. If your idea is to create multiple 5 multiple queues and supervisors to accomplish, that's not a standard way to do it I think.

关于在这种情况下可以做什么的任何想法?

Any Idea on what can be done in this case?

推荐答案

只需添加一些我的个人经验即可.

Just to add something from my personal experience.

首先为您的操作系统安装并配置管理器,以下是linux basaed OS的confs Ubuntu

First install and configure supervisor for your OS accordingly, following are the confs for linux basaed OS e.g. Ubuntu

Supervisor conf:(/etc/supervisor/conf.d)

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/app/artisan queue:work database --sleep=3 --tries=3
autostart=true
autorestart=true
user=username
numprocs=25
redirect_stderr=true
stderr_events_enabled=true
stderr_logfile=/var/www/app/storage/logs/worker.error.log
stdout_logfile=/var/www/app/storage/logs/worker.log

然后根据您的需求和主管将同时处理作业,在这种情况下,25作业将同时处理.

Supervisor will process jobs simultaneously, in this particular case 25 jobs will be processing at a time.

这篇关于如何在Laravel中执行多个后台进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆