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

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

What is the difference between queue:work --daemon and queue:listen

laravellaravel-4laravel-5

提问by Logan Bailey

I'm settin up my offline job server. I've read the documentation but I still don't really see the differences between the two commands: artisan queue:work --daemonand artisan queue:listen. Which command should I use for running my daemons?

我正在设置我的离线作业服务器。我已经阅读了文档,但我仍然没有真正看到两个命令之间的区别:artisan queue:work --daemonartisan queue:listen。我应该使用哪个命令来运行我的守护进程?

回答by Laurence

Edit updated 2017-04-07:

编辑更新 2017-04-07:

There are now three ways to run your queue:

现在有三种方法可以运行您的队列:

  • queue:work- this is the new "daemon" process (the flag is no longer required). The framework will fire up "once" - and then keep looping through the jobs. This will continue indefinitely. It uses less memory/cpu than queue:listenbecause the framework stays up the entire time. You must also remember to use queue:restartto force the queue to update any code changes you push during patching.

  • queue:work --once- this will fire up the framework, process one job, then shutdown. Useful for testing during development etc.

  • queue:listen- this will fire the framework up on every cycle, process one job, then fully shutdown, and then fire the framework up again etc and loop indefinitely. This means all memory/processes are released after each job is processed. If you have memory leaks with queue:work- give this a try.

  • queue:work- 这是新的“守护进程”进程(不再需要该标志)。该框架将启动“一次” - 然后继续循环遍历作业。这将无限期地持续下去。它使用更少的内存/cpu,queue:listen因为框架一直保持运行。您还必须记住使用queue:restart强制队列更新您在修补期间推送的任何代码更改。

  • queue:work --once- 这将启动框架,处理一项工作,然后关闭。用于开发过程中的测试等。

  • queue:listen- 这将在每个周期启动框架,处理一项工作,然后完全关闭,然后再次启动框架等并无限循环。这意味着在处理每个作业后释放所有内存/进程。如果你有内存泄漏queue:work- 试试这个。

The --daemonflag no longer has an effect on these commands.

--daemon标志不再对这些命令产生影响。

Original answer:

原答案:

There are two different issues listed.

列出了两个不同的问题。

There is artisan queue:workand artisan queue:listen

artisan queue:workartisan queue:listen

  • queue:workwill 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:listenwill listen to the queue, and continue to process any queue commands it receives. This will continue running indefinitely until you stop it.
  • queue:work将简单地弹出队列中的下一个作业,并仅处理该作业。这是一个“一次性”命令,一旦处理完一个队列命令,它将返回命令提示符。
  • queue:listen将侦听队列,并继续处理它收到的任何队列命令。这将无限期地继续运行,直到您停止它。

In Laravel >=4.2 there was a --daemoncommand 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 significantlyreduces the memory and cpu requirements of your queue.

在 Laravel >=4.2 中--daemon添加了一个命令。它的工作方式是直接直接运行队列,而不是在处理完每个队列后重新启动整个框架。这是一个可选命令,可显着降低队列的内存和 CPU 需求。

The important point with the --daemoncommand 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

回答by Alupotha

Things have been changedbut it was not mentioned in the document

事情已经改变,但文件中没有提到

 --daemon  Run the worker in daemon mode (Deprecated)

now by defaultphp artisan queue:workruns in daemon mode,

现在默认php artisan queue:work以守护程序模式运行,

so queue:workcontinue processing jobs without ever re-booting the framework

因此queue:work无需重新启动框架即可继续处理作业

for run it oncecommand is,

运行一次命令是,

php artisan queue:work --once

php artisan queue:work --once

回答by Simon Fredsted

The most important difference is that queue:work --daemondoes not restart the framework on each job, but queue:listendoes. In fact, listenstarts a whole new Laravel process for each job.

最重要的区别是queue:work --daemon不会在每个作业上重新启动框架,而是queue:listen。实际上,listen为每个作业启动一个全新的 Laravel 进程。

Try for yourself: Open 2 terminals and run work --daemonin one and listenin the other. The workwindow will execute jobs much faster than listen.

自己尝试:打开 2 个终端并work --daemon在其中一个和listen另一个中运行。该work窗口将比listen.

回答by PtrTon

As of Laravel 5.7 a new option --stop-when-emptyhas been added to the queue:workcommand. When using this option the current queue will be processed until it's empty, then the command will exit.

从 Laravel 5.7--stop-when-empty开始,queue:work命令中添加了一个新选项。使用此选项时,当前队列将被处理,直到它为空,然后命令将退出。

According to the documentation:

根据文档

The --stop-when-empty option may be used to instruct the worker to process all jobs and then exit gracefully. This option can be useful when working Laravel queues within a Docker container if you wish to shutdown the container after the queue is empty:

--stop-when-empty 选项可用于指示工作人员处理所有作业,然后正常退出。如果您希望在队列为空后关闭容器,则此选项在 Docker 容器中处理 Laravel 队列时非常有用:

php artisan queue:work --stop-when-empty