laravel queue:work 和 queue:listen 有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41991251/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:14:40  来源:igfitidea点击:

What is the difference queue:work and queue:listen

laravel

提问by MisterCat

I can't understand what's the difference between Laravel queue:workand Laravel queue:listen

我不明白 Laravelqueue:work和 Laravel 有什么区别queue:listen

I can see that:

我知道:

  • Queue: Listen to a given queue
  • Work: Process the next job on a queue
  • 队列:收听给定的队列
  • 工作:处理队列中的下一个工作

But still don't get it, because I've tried both, both will run queue if there is any new queue ("work option" not just running once)

但还是不明白,因为我都试过了,如果有新队列,两者都会运行队列(“工作选项”不只是运行一次)

I'm not talking about the daemon option. Just these both.

我不是在谈论守护进程选项。就这两个。

when i ran queue:work it works like listen

当我运行 queue:work 它就像听

回答by Ohgodwhy

Until Laravel 5.2you had :listenand :work.

直到 Laravel5.2你有:listen:work

Workwould process the first job in the queue.

Work将处理队列中的第一个作业。

Listenwould process all jobs as they came through.

Listen将处理所有工作。

In Laravel 5.3+ this is no longer the case. Listenstill exists, but it is deprecated and slated for removal in 5.5. You should prefer :worknow.

在 Laravel 5.3+ 中,情况不再如此。Listen仍然存在,但它已被弃用并计划在5.5. 你:work现在应该更喜欢。

Worknow process jobs one after the other, but have a plethora of options you can configure.

Work现在一个接一个地处理作业,但您可以配置大量选项。

Edit

编辑

The above was true at the time of the posting, but since then things have been changed a bit.

以上在发布时是正确的,但从那时起事情已经发生了一些变化。

queue:workshould be preferred when you want your queue's to run as a daemon. This would be a long-lived process that would be beneficial where performance was an issue. This will use a cached version of the application and does not re-bootstrap the application every time a job is processed.

queue:work当您希望队列作为daemon. 这将是一个长期存在的过程,在性能存在问题的情况下将是有益的。这将使用应用程序的缓存版本,并且不会在每次处理作业时重新引导应用程序。

queue:listenshould be used when you don't care about performance or you don't want to have to restart the queue after making changes to the code.

queue:listen当您不关心性能或者您不想在更改代码后重新启动队列时,应该使用它。

  • They'll both pop jobs off the queue 1-by-1 as received.
  • They both share almostthe exact same options that can be passed to them.
  • 它们都将按接收到的方式从队列中 1 对 1 弹出作业。
  • 它们共享几乎可以传递给它们的完全相同的选项。

回答by ShahinSorkh

In Laravel 5.3+ queue:work runs a daemon listener. It could in 5.2 as well if you specified the --daemonflag. A daemon work boots the framework one timeand then processes jobs repeatedly. The queue:listencommand runs a queue:work --oncesub-process in a loop which boots the framework each iteration.

在 Laravel 5.3+ queue:work 运行守护进程监听器。如果您指定了--daemon标志,它也可以在 5.2 中使用。守护进程工作引导框架一次,然后重复处理作业。该queue:listen命令queue:work --once在循环中运行一个子进程,每次迭代都会启动框架

queue:workshould pretty much always be used in production as it's much more efficient and uses less RAM. However; you need to restart it after each core change. queue:listenis useful for development and local environments because you don't have to restart it after code changes (because the framework is booting fresh each job).

queue:work应该几乎总是在生产中使用,因为它更高效并且使用更少的 RAM。然而; 您需要在每次核心更改后重新启动它。queue:listen对开发和本地环境很有用,因为您不必在代码更改后重新启动它(因为框架正在重新启动每个作业)。

from here

从这里

回答by Erik Berkun-Drevnig

The queue:workArtisan command includes a --daemonoption for forcing the queue worker to continue processing jobs without ever re-booting the framework. This results in a significant reduction of CPU usage when compared to the queue:listen command:

As you can see, the queue:workjob supports most of the same options available to queue:listen. You may use the php artisan help queue:workjob to view all of the available options.

queue:work工匠命令包括--daemon用于迫使队列工作继续处理工作而没有重新启动的框架选项。与 queue:listen 命令相比,这会显着降低 CPU 使用率:

如您所见,该queue:work作业支持可用于queue:listen. 您可以使用 php artisan helpqueue:work作业查看所有可用选项。

https://laravel.com/docs/5.1/queues#running-the-queue-listener

https://laravel.com/docs/5.1/queues#running-the-queue-listener

回答by Khaled karam

There are two different issues listed.

列出了两个不同的问题。

There is artisan queue:work and artisan queue:listen

有工匠队列:工作和工匠队列:听

queue:work will simply pop off the next job in the queue, and process only that one job. This is a 'one off' command that will return to the command prompt once the one queue command is processed. queue:listen will listen to the queue, and continue to process any queue commands it receives. This will continue running indefinitely until you stop it. In Laravel >=4.2 there was a --daemon command added. The way it works is simply keeps running the queues directly, rather than rebooting the entire framework after every queue is processed. This is an optional command that significantly reduces the memory and cpu requirements of your queue.

queue:work 将简单地弹出队列中的下一个作业,并仅处理该作业。这是一个“一次性”命令,一旦处理完一个队列命令,它将返回到命令提示符。queue:listen 将侦听队列,并继续处理它收到的任何队列命令。这将无限期地继续运行,直到您停止它。在 Laravel >=4.2 中添加了一个 --daemon 命令。它的工作方式是直接直接运行队列,而不是在处理完每个队列后重新启动整个框架。这是一个可选命令,可显着降低队列的内存和 CPU 需求。

The important point with the --daemon command is that when you upgrade your application, you need to specifically restart your queue with queue:restart, otherwise you could potentially get all sorts of strange errors as your queue would still have the old code in memory.

--daemon 命令的重点是,当您升级应用程序时,您需要专门使用 queue:restart 重新启动您的队列,否则您可能会遇到各种奇怪的错误,因为您的队列仍将在内存中保留旧代码.

So to answer your question "Which command should I use for running my daemons?" - the answer is almost always queue:work --daemon

所以要回答你的问题“我应该使用哪个命令来运行我的守护进程?” - 答案几乎总是 queue:work --daemon