nodejs为什么要等待很长时间才退出

发布于 2025-02-12 01:22:56 字数 1906 浏览 1 评论 0原文

我尝试编写一个创建操作员,以从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 技术交流群。

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

发布评论

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

评论(1

感受沵的脚步 2025-02-19 01:22:56

简短的答案是很多

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.

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