nodejs为什么要等待很长时间才退出
我尝试编写一个创建操作员,以从Polkadot.js上的WSprovider获得可观察的。 并尝试参加Polkadot活动。
这是代码
import {from, fromEvent, of,Observable} from 'rxjs';
import {tap,mergeMap} from "rxjs/operators"
import { WsProvider } from '@polkadot/api';
console.time("main")
const urls = of("wss://192")
const fromWs = (url:string) => {
const ws = new WsProvider(url,0);
ws.connect().then(val => console.timeLog("main",val)).catch(console.error)
return new Observable((subscriber) => {
ws.on("disconnected",()=>{
console.timeLog("main","wss disconnected")
subscriber.next(url);
subscriber.complete()
})
ws.on('error', () =>{
console.timeLog("main","wss error")
subscriber.complete()
})
ws.on('connected' ,() => {
console.timeLog("main", `${url} connected`)
})
setTimeout(() => {
console.timeLog("main", `${url} timeout`)
ws.disconnect().then(val=> console.timeLog("main",'ws disconnect')).catch(err => console.error("disconnect error"))
subscriber.complete()
},2000)
})
}
urls.pipe(
tap(console.log),
mergeMap( (url:string) =>{
return fromWs(url)
}),
).subscribe({
next: val => console.timeLog("main", 'subcrnext'),
error: err=> console.timeLog("main", "sub error"),
complete: () => console.timeLog("main", "sub complete")
})
process.on('exit', (code) => {
console.timeLog("main",'System exit');
});
,我尝试通过TS节点执行它。
➜ rxjsexample ts-node src/main.ts
wss://192
main: 11.959ms undefined
main: 2.013s wss://192 timeout
main: 2.015s wss error
main: 2.016s sub complete
main: 2.017s wss disconnected
main: 2.018s ws disconnect
main: 2:10.176 (m:ss.mmm) System exit
在2.018S时,网络连接是断开连接的。 但是在2M10S时,Nodejs只是退出。
在此期间,Nodejs在做什么?
i try to write a Creation Operator to got a observable from a wsprovider on polkadot.js.
and try to got polkadot event.
this is the code
import {from, fromEvent, of,Observable} from 'rxjs';
import {tap,mergeMap} from "rxjs/operators"
import { WsProvider } from '@polkadot/api';
console.time("main")
const urls = of("wss://192")
const fromWs = (url:string) => {
const ws = new WsProvider(url,0);
ws.connect().then(val => console.timeLog("main",val)).catch(console.error)
return new Observable((subscriber) => {
ws.on("disconnected",()=>{
console.timeLog("main","wss disconnected")
subscriber.next(url);
subscriber.complete()
})
ws.on('error', () =>{
console.timeLog("main","wss error")
subscriber.complete()
})
ws.on('connected' ,() => {
console.timeLog("main", `${url} connected`)
})
setTimeout(() => {
console.timeLog("main", `${url} timeout`)
ws.disconnect().then(val=> console.timeLog("main",'ws disconnect')).catch(err => console.error("disconnect error"))
subscriber.complete()
},2000)
})
}
urls.pipe(
tap(console.log),
mergeMap( (url:string) =>{
return fromWs(url)
}),
).subscribe({
next: val => console.timeLog("main", 'subcrnext'),
error: err=> console.timeLog("main", "sub error"),
complete: () => console.timeLog("main", "sub complete")
})
process.on('exit', (code) => {
console.timeLog("main",'System exit');
});
and i try to execute it by ts-node.
➜ rxjsexample ts-node src/main.ts
wss://192
main: 11.959ms undefined
main: 2.013s wss://192 timeout
main: 2.015s wss error
main: 2.016s sub complete
main: 2.017s wss disconnected
main: 2.018s ws disconnect
main: 2:10.176 (m:ss.mmm) System exit
at 2.018s, the network conenct is disconnect.
but at 2m10s, the nodejs just exits.
What is nodejs doing during this time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的答案是很多。
node.js
是一个基于事件的运行时,它通过在每个循环处的一组前缀相位。执行计时器功能后,不应再有事件提供商(计时器,i/o ...),并且运行时将通过清理阶段执行任何
关闭
事件处理程序以及进程#退出事件。因此,在计时器函数执行和内部检查/清理之间,仍需要执行语句,这将导致您在最后一个 userland 计时器函数与最后一个 userland
退出
事件处理程序。The short answer is a lot.
node.js
is an event-loop based runtime which goes through a set of prefixed phases at each loop.Once your timer function is executed, there should be no more event providers (timers, I/O...) and the runtime will go through a cleanup phase executing any
close
event handlers along with the process#exit event.Hence between the timer function execution and the internal checking / cleanup, there still statements to execute which will lead to the time laps you are seeing between the last userland timer function and the last userland
exit
event handler.