最近学习了下laravel的队列内容,碰到了在消费队列时可以使用queue:work
和queue:listen
来处理。那么这2个命令有什么样的本质区别呢?
在Laravel>5.3以后的版本中
queue:work
将自动开启后台进程不再需要使用—daemon
进行标记,这种方式运行,框架只会启动一次,并保持循环去消费队列,除非出现异常否则该进程将无限时间运行下去。这种方式消耗的cpu
和内存
都比queue:listen
要少,因为在整个生命周期中框架一直是在保持运行状态。同时,使用该方法时如果更新了代码,记得使用queue:restart
来重启。queue:work --once
- 该方法会启动框架,运行 job,然后销毁掉。在开发和测试代码的时候使用比较合适,因为每次都会加载一遍代码。queue:listen
- 这种方式运行,框架每次都会启动,运行job,然后关闭,然后再次启动框架,运行job,然后关闭,这样一直循环(每次运行完一次都会完全释放掉运行时的内存和进程)。所以这种方式你不用担心代码的热更新,不用去重启queue
,随之而来的另外一个好处是不用去担心queue:work
带来的内存泄漏。
注意 从5.3
版本开始--daemon
这个参数已经不再起作用了,可以看Illuminate\Queue\Console\WorkCommand.php
protected $signature = 'queue:work
{connection? : The name of connection}
{--queue= : The queue to listen on}
{--daemon : Run the worker in daemon mode (Deprecated)}
{--once : Only process the next job on the queue}
{--delay=0 : Amount of time to delay failed jobs}
{--force : Force the worker to run even in maintenance mode}
{--memory=128 : The memory limit in megabytes}
{--sleep=3 : Number of seconds to sleep when no job is available}
{--timeout=60 : The number of seconds a child process can run}
{--tries=0 : Number of times to attempt a job before logging it failed}';