基于事件==异步?
“基于事件”与“异步”相同吗?
Is "event based" the same as "asynchronous"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
“基于事件”与“异步”相同吗?
Is "event based" the same as "asynchronous"?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
不,这并不意味着事件是异步的。
在事件驱动的单线程系统中,您可以触发事件,但它们都是串行处理的。作为处理的一部分,它们可能会屈服,但不会同时发生任何事情,如果它们屈服,它们就会停止处理,并且必须等到再次收到消息才能再次开始处理。
这方面的示例有 Swing ( Java )、Twisted ( Python )、Node.js ( JavaScript )、EventMachine ( Ruby )
所有这些示例都是事件驱动的消息循环,但它们都是单线程的,每个事件都会阻塞所有后续事件同一个线程。
在编程中,异步事件是那些独立于主程序流程发生的事件。异步操作是以非阻塞方案执行的操作,允许主程序流继续处理。
因此,仅仅因为某些东西是
事件驱动
并不意味着它异步
,并且仅仅因为某些东西是异步
并不意味着它是事件驱动
;更不用说并发了。No it doesn't imply that the events are asynchronous.
In a event driven single threaded system, you can fire events, but they are all processed serially. They may yield as part of their processing, but nothing happens
concurrently
, if they yield, they stop processing and have to wait until they are messaged again to start processing again.Examples of this are Swing ( Java ), Twisted ( Python ), Node.js ( JavaScript ), EventMachine ( Ruby )
All of these examples are event driven message loops, but they are all single threaded, every event will block all subsequent events on that same thread.
In programming, asynchronous events are those occurring independently of the main program flow. Asynchronous actions are actions executed in a non-blocking scheme, allowing the main program flow to continue processing.
So just because something is
event driven
doesn't make itasynchronous
, and just because something isasynchronous
doesn't make itevent driven
either; much less concurrent.它们本质上是正交的概念。
“事件驱动”本质上意味着与某些函数调用关联的代码在运行时绑定(并且可以通过执行进行更改)。
谁“触发”事件不知道什么将处理它,谁处理事件被定义为通过程序执行时定义的关联来响应事件。通常,虽然函数指针、引用或指向携带虚拟方法的对象的指针等)
“异步”意味着程序流在继续之前不必等待执行调用(主要是通过将执行委托给另一个线程或进程后立即返回的调用)
并非所有事件都是异步的(想想 Windows
SendMessage
,参考PostMessage
),并且并非所有事件都是异步的调用需要通过“事件”来实现(虽然使用事件机制来实现异步调用是很常见的)They are essentially orthogonal concepts.
"event driven" essentially means that the code associated to certain function calls is bind at runtime (and can change through the execution).
Who "fires" the event doesn't know what will handle it, and who handle the event is defined to respond to the event through an association defined while the program executes. Typically though function pointers, reference or pointers to object carrying virtual methods etc.)
"asynchronous" means that a program flow doesn't have to wait for a call to be executed before proceed over (mostly implemented with a call that returns immediately after delegating the execution to another thread or process)
Not all events are asynchronous (think to the windows
SendMessage
, respect toPostMessage
), and not all asynchronous calls are necessary implemented by "events" (although the use of the event mechanism is quite common to implement asynchronous calls)异步的一个含义是,在发出计算时,您不会等待答案,而是稍后得到答案。答案与您的正常控制流程正交。
答案的一种方式是使用事件:在这种情况下它们会自发发生,而不需要您的代码触发它们。在处理程序中,您可以处理结果。
对于同步模式,计算和答案是通过控制流中的点连接的,而在异步模式下,您必须自己进行连接。例如通过使用序列号或其他东西。
One meaning of asynchronous is that at a point where you emit an computation, you don't wait for an answer, but you get the answer later. The answer comes in orthogonal to you normal control flow.
One way the answer comes in is using events: They happen spontaneously in this case, without your code triggering them. In a handler you may process the result.
Whereas the computation and answer is connected by the point in control flow for synchronous mode, you have to do the connection yourself in asynchronous mode. For example by use of a sequence number or something.