Laravel 事件、监听器、作业、队列之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36106532/
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
Laravel difference between Events, Listeners, Jobs, Queues
提问by Taylor
It sounds extremely confusing to me, what are the differences? Can someone please do an ELI5?
这对我来说听起来非常混乱,有什么区别?有人可以做一个ELI5吗?
回答by stratedge
Although they all can work together, I find it's easiest to look at Events and Listeners together, and then Jobs and Queues together.
尽管它们都可以一起工作,但我发现最简单的方法是将事件和侦听器放在一起,然后将作业和队列放在一起。
Events and Listeners
事件和监听器
Events are objects that hold data that are "fired", the Laravel event system "catches" the event object when it is fired, and then all the Listeners that are registered for that particular event are run.
事件是保存“触发”数据的对象,Laravel 事件系统在它被触发时“捕获”事件对象,然后运行为该特定事件注册的所有侦听器。
If you think about it, this is akin to how exceptions work. You throw an exception, and you can define several catch blocks to react depending on which exception is thrown. In the case of Events and Listeners, an Event is thrown and one or more Listeners represent the contents of a catch block. Though similar, Events and Listeners are not error handlers, they just have conceptual similarities.
如果您考虑一下,这类似于异常的工作方式。你抛出一个异常,你可以定义几个 catch 块来根据抛出的异常做出反应。在事件和监听器的情况下,一个事件被抛出,一个或多个监听器代表一个 catch 块的内容。虽然相似,但事件和侦听器不是错误处理程序,它们只是在概念上有相似之处。
Jobs and Queues
作业和队列
I think the best way to think of these is like a line at a bank. The line itself is the Queue, and each customer in the line is a Job.
我认为考虑这些的最佳方式就像银行的一条线。线路本身就是队列,线路中的每个客户都是一个作业。
In order to process Jobs in the Queue you need command line processes or daemons. Think of launching a queue daemon on the command line as adding a new bank teller to the pool of available bank tellers. When a daemon is available it will ask the Queue for the next Job like the bank teller asking for the next person in line to step to the window.
为了处理队列中的作业,您需要命令行进程或守护进程。将在命令行上启动队列守护程序视为将新的银行柜员添加到可用的银行柜员池中。当守护进程可用时,它将向队列询问下一个作业,就像银行出纳员要求排队的下一个人走到窗口一样。
Each person in line has a specific task they want accomplished, like making a deposit or withdrawal. The action that the person in line needs completed is the Worker in Laravel.
排队的每个人都有他们想要完成的特定任务,例如存款或取款。排队的人需要完成的动作是 Laravel 中的 Worker。
The Worker is the thing the daemon will do for the Job that was taken from the Queue, just like the task is the thing the bank teller will do for the customer who stepped forward from the line.
Worker 是守护程序将为从队列中取出的 Job 所做的事情,就像任务是银行出纳员将为从队列中走出来的客户所做的事情一样。
Hope any of that makes some sense.
希望其中任何一个都有意义。