Laravel 中的多个队列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50576569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple Queues in Laravel
提问by Gaganpreet Kaur
I am creating a web application in laravel in which bidding is being done by users in multiple games. Bidding is being performed by front end users and by cron job as well. Cron job do bid on each game after each second. Therefore some collision was happening between bids when same row was accessed at same time. To resolve concurrency issue I decided to use laravel queues for bidding. But I am having multiple games and therefore I want simultaneously bids of each game. I don't want bids of same game to be process at same time because then concurrency issue can be occur. I want to know about multiple queues system in laravel. After having search on internet I got to know about multiple queues like
我正在 Laravel 中创建一个 Web 应用程序,其中多个游戏中的用户正在竞标。投标由前端用户和 cron 作业执行。Cron 工作在每一秒之后对每场比赛进行竞标。因此,当同时访问同一行时,出价之间会发生一些冲突。为了解决并发问题,我决定使用 Laravel 队列进行竞价。但是我有多场比赛,因此我希望同时对每场比赛进行出价。我不希望同时处理同一游戏的出价,因为可能会出现并发问题。我想了解 Laravel 中的多队列系统。在互联网上搜索后,我了解了多个队列,例如
php artisan queue:work --queue=myJobQueue, myJobQueue1, myJobQueue2,..myJobQueue7
But I am not sure how it works. Please someone explain me in detail that all 7 queues work simultaneously or one by one.
但我不确定它是如何工作的。请有人详细解释一下,所有7个队列同时或一个一个地工作。
回答by Ben V.
Are looking for the queue:listencommand?
正在寻找queue:listen命令?
queue:workwill process all pending jobs that are stored by the queue driver, whereas queue:listenwill be waiting for jobs to be thrown at it to execute them as they come.
queue:work将处理队列驱动程序存储的所有待处理作业,而queue:listen将等待作业被抛出以在它们到来时执行它们。
If you do php artisan queue:listen --queue=myJobQueue, myJobQueue1, myJobQueue2,..myJobQueue7, 7 queues are being created and listening to new tasks on their own.
如果您这样做php artisan queue:listen --queue=myJobQueue, myJobQueue1, myJobQueue2,..myJobQueue7,将创建 7 个队列并自行侦听新任务。
In your code, you can dispatch jobs like the following:
在您的代码中,您可以分派如下作业:
dispatch((new MyJob)->onQueue('myJobQueue'));
You might want to use a tool like Supervisorto make sure queue:listenis always running in the background.
您可能想要使用像Supervisor这样的工具来确保queue:listen始终在后台运行。
Hope this helps!
希望这可以帮助!
回答by Jfizzle
php artisan queue:work --queue=myJobQueue, myJobQueue1, myJobQueue2,..myJobQueue7sets the priority in which queues will be executed. So with this all jobs on myJobQueuewill be executed before moving to execute jobs on myJobQueue1then to myJobQueue2in that order.
php artisan queue:work --queue=myJobQueue, myJobQueue1, myJobQueue2,..myJobQueue7设置队列将被执行的优先级。因此,有了这个,所有作业myJobQueue都将在执行作业之前执行,myJobQueue1然后按myJobQueue2顺序执行。
However if you want jobs on these queues to be executed simultaneously, you could run each queue in the background.
但是,如果您希望同时执行这些队列上的作业,则可以在后台运行每个队列。
php artisan queue:work --queue=myJobQueue & php artisan queue:work --queue=myJobQueue1 & php artisan queue:work --queue=myJobQueue2 &
php artisan queue:work --queue=myJobQueue & php artisan queue:work --queue=myJobQueue1 & php artisan queue:work --queue=myJobQueue2 &
This will run each queue as single processes in the background.
这将在后台将每个队列作为单个进程运行。
回答by kregus
Like Ben V said, it is highly recommended to use Supervisorto keep the workers active at all times, especially if you want to run one or more workers per queue, or if you want the queues to be processed simultaneously.
就像 Ben V 所说的那样,强烈建议使用Supervisor始终保持工作线程处于活动状态,特别是如果您想为每个队列运行一个或多个工作线程,或者希望同时处理队列。
Here is an example Supervisor configuration file:
这是一个示例 Supervisor 配置文件:
[program:laravel-worker-myJobQueue]
process_name=%(program_name)s_%(process_num)s
command=php artisan queue:work --queue=myJobQueue
numprocs=8
autostart=true
autorestart=true
[program:laravel-worker-myJobQueue1]
process_name=%(program_name)s_%(process_num)s
command=php artisan queue:work --queue=myJobQueue1
numprocs=1
autostart=true
autorestart=true
The above configuration creates 8 workers for myJobQueue, and one worker for myJobQueue1, since multiple workers can help speed things up, but can cause trouble for jobs that try to access the same row in the database, in which case you want to limit things to 1 worker only.
上面的配置为 创造了 8 个工作人员,为 创造了myJobQueue一个工作人员myJobQueue1,因为多个工作人员可以帮助加快速度,但可能会给尝试访问数据库中同一行的作业带来麻烦,在这种情况下,您希望将事情限制为 1 个工作人员只要。
You then simply dispatch jobs to the correct queue using
然后,您只需使用以下命令将作业分派到正确的队列
dispatch((new MyJob)->onQueue('myJobQueue'));
or
或者
dispatch((new MyJob)->onQueue('myJobQueue1'));

