Node.js-单线程,非阻塞? [英] Node.js - single thread, non-blocking?

查看:46
本文介绍了Node.js-单线程,非阻塞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Node.js,并且已经阅读到Node.js是单线程且无阻塞的.

I am learning Node.js and I have read that Node.js is single threaded and non-blocking.

我在JavaScript方面有很好的背景,并且确实了解回调,但是我真正不了解的是Node.js如何实现单线程并在后台运行代码.那不是矛盾的吗?

I have a good background in JavaScript and I do understand the callbacks, but what I don't really understand is how Node.js can be single threaded and run code in the background. Isn't that contradictory?

因为Node.js是单线程的,所以它当时仍只能执行一项任务.因此,如果它在后台运行某些内容,则必须停止当前任务以在后台处理某些内容,对吧?

Because if Node.js is single threaded it can still only perform one task at the time. So if it runs something in the background it has to stop the current task to process something in the background, right?

这实际上如何工作?

推荐答案

就NodeJS而言,后台"的真正含义是将事情放到待办事项列表中以备后用.只要Node完成其工作,它就会从待办事项列表的顶部选择.这就是为什么做任何实际上被阻止的事情都会破坏您的一天的原因.在后台执行的所有操作(实际上只是在待办事项列表上等待)都将停止,直到阻止任务完成为止.

What "in the background" really means in terms of NodeJS is that things get put on a todo list for later. Whenever Node is done with what it's doing it picks from the top of the todo list. This is why doing anything that actually IS blocking can wreck your day. Everything that's happening "in the background" (actually just waiting on the todo list) gets stopped until the blocking task is complete.

这篇关于Node.js-单线程,非阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆