基于事件==异步?

发布于 2024-12-12 08:36:19 字数 22 浏览 0 评论 0原文

“基于事件”与“异步”相同吗?

Is "event based" the same as "asynchronous"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光①夏 2024-12-19 08:36:19

不,这并不意味着事件是异步的。

在事件驱动的单线程系统中,您可以触发事件,但它们都是串行处理的。作为处理的一部分,它们可能会屈服,但不会同时发生任何事情,如果它们屈服,它们就会停止处理,并且必须等到再次收到消息才能再次开始处理。

这方面的示例有 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 it asynchronous, and just because something is asynchronous doesn't make it event driven either; much less concurrent.

只是一片海 2024-12-19 08:36:19

它们本质上是正交的概念。

事件驱动”本质上意味着与某些函数调用关联的代码在运行时绑定(并且可以通过执行进行更改)。
谁“触发”事件不知道什么将处理它,谁处理事件被定义为通过程序执行时定义的关联来响应事件。通常,虽然函数指针、引用或指向携带虚拟方法的对象的指针等)

“异步”意味着程序流在继续之前不必等待执行调用(主要是通过将执行委托给另一个线程或进程后立即返回的调用)

并非所有事件都是异步的(想想 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 to PostMessage), and not all asynchronous calls are necessary implemented by "events" (although the use of the event mechanism is quite common to implement asynchronous calls)

一人独醉 2024-12-19 08:36:19

异步的一个含义是,在发出计算时,您不会等待答案,而是稍后得到答案。答案与您的正常控制流程正交。

答案的一种方式是使用事件:在这种情况下它们会自发发生,而不需要您的代码触发它们。在处理程序中,您可以处理结果。

对于同步模式,计算和答案是通过控制流中的点连接的,而在异步模式下,您必须自己进行连接。例如通过使用序列号或其他东西。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文