简体   繁体   English

同步触发所有事件处理程序是什么意思?

[英]What does it mean that all event handlers are fired synchronously?

I am confused about some terms.我对某些术语感到困惑。 I am trying to find out how the event system of Node.js actually works, and in a lot of places I read that the event handlers are totally synchronous.我试图找出 Node.js 的事件系统实际上是如何工作的,并且在很多地方我读到事件处理程序是完全同步的。

For me that seemed really strange, because one of the advantages of using an event-driven approach would be that the main thread would not be blocked by events.对我来说这似乎很奇怪,因为使用事件驱动方法的优点之一是主线程不会被事件阻塞。 So I tried to come up with my own example, and it seems like that what did happen was what I actually expected:所以我试着想出我自己的例子,看起来确实发生的事情是我真正期望的:

const fs = require('fs')
const util = require('util')
const readFile = util.promisify(fs.readFile)

const events = require('events')
const emitter = new events.EventEmitter()

emitter.on('fire', () => {
  readFile('bigFile.txt')
    .then(() => console.log('Done reading bigFile.txt'))
    .catch(error => console.log(error))
  console.log('Sync thing in handler')
})

emitter.on('fire', () => {
  console.log('Second handler')
})

console.log('First outside')
emitter.emit('fire')
console.log('Last outside')

Note that bigFile.txt is an actually large text file, processing it takes a few hundred milliseconds on my machine.请注意, bigFile.txt实际上是一个很大的文本文件,在我的机器上处理它需要几百毫秒。

Here I first log out 'First outside' synchronously.在这里,我首先同步注销“First outside”。 Then I raise the event which starts the event handling process.然后我提出启动事件处理过程的事件。 The event handler does seem to be asynchronous, because even though we first log out the synchronous 'Sync thing in handler' text, we start using the thread pool in the background to return back with the result of reading the file later.事件处理程序似乎是异步的,因为即使我们首先注销同步的“处理程序中的同步事物”文本,我们还是开始在后台使用线程池来返回稍后读取文件的结果。 After running the first handler, the second handler runs printing out its message, and finally we print out the last sync message, 'Last outside'.运行第一个处理程序后,第二个处理程序运行打印出它的消息,最后我们打印出最后一个同步消息,'Last outside'。

So I started with trying to prove what some people say, which is that event handlers are by nature synchronous, and then I found them to be asynchronous.所以我开始试图证明一些人所说的,即事件处理程序本质上是同步的,然后我发现它们是异步的。 My best guess is that either people saying that the event system is synchronous mean something else, or that I have some conceptual misunderstanding.我最好的猜测是,要么人们说事件系统是同步的,要么是我有一些概念上的误解。 Please help me understand this issue!请帮助我理解这个问题!

The EventEmitter class is synchronous in regard to the emit function: event handlers are called synchronously from within the .emit() call, as you've demonstrated with the fire event you fired yourself. EventEmitter class 在emit function 方面是同步的:事件处理程序是从.emit .emit()调用中同步调用的,正如您在自己触发的fire事件中所展示的那样。

In general, events that come from the operating system (file and network operations, timers etc) through node's event loop are fired asynchronously.通常,来自操作系统的事件(文件和网络操作、计时器等)通过节点的事件循环被异步触发 You're not firing them yourself, some native API does fire them.你不是自己开火,一些本地 API 确实开火了。 When you listen to these events, you can be sure that they will occur not before the next tick .当您收听这些事件时,您可以确定它们不会在下一个滴答之前发生

The event handler does seem to be asynchronous, because even though we first log out the synchronous 'Sync thing in handler' text, we start using the thread pool in the background to return back with the result of reading the file later事件处理程序似乎是异步的,因为即使我们首先注销同步的“处理程序中的同步事物”文本,我们还是开始在后台使用线程池返回稍后读取文件的结果

Yes, you are calling the asynchronous function readFile (that will notify you later), but that doesn't make your event listener function or the .emit('fire') call asynchronous.是的,您正在调用异步 function readFile (稍后会通知您),但这不会使您的事件侦听器 function 或.emit('fire')调用异步。 Even "asynchronous functions" that start a background process will immediately (synchronously) return something - often nothing ( undefined ) or a promise.即使是启动后台进程的“异步函数”也会立即(同步)返回一些东西——通常什么都没有( undefined )或 promise。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检查文件是否同步存在是什么意思? - What does it mean to check if a file exists synchronously? JavaScript中的事件处理程序是否以FIFO,LIFO或并行方式触发一个事件? - Does Events Handlers in JavaScript for one event fired as FIFO, LIFO or in parallel? 为什么事件处理程序被触发两次? - Why are the event handlers fired twice? 一旦事件触发,即使我`$ .unbind('event')`也触发所有处理程序。 有没有办法防止事件发生后事件处理程序触发? - Once event fired even if I `$.unbind('event')` all handlers fire. Is there a way to prevent event handlers firing, after event occured? 在浏览器中触发事件时,事件处理程序在什么时候确定? - At what point do the event handlers get determined when an event has been fired in the browser? 事件绑定是什么意思? - What does event binding mean? 我们可以将所有所有信息作为参数传递给内联事件处理程序吗? - What all information we can pass as parameters to inline event handlers? 成功事件未触发 - Success Event does not Fired 在ExtJs 3中,触发后渲染事件后,组件会发生什么情况? - In ExtJs 3 what does happen to a component after afterrender event is fired? Angular2:绑定事件为0是什么意思? - Angular2: What does binding event to 0 mean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM