Laravel 消费队列的正确使用姿势
目前版本 ( version >= 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}';
本作品采用《CC 协议》,转载必须注明作者和本文链接
本帖由 Summer
于 7年前 加精
那是用哪种方式号
@Jea 看什么环境。本地开发就是用listen,生产环境用work
queue:work 带来的内存泄漏问题,请问有好的解决方案吗