When is Node.js blocking?

后端 未结 3 919
名媛妹妹
名媛妹妹 2021-01-02 11:13

I have used Node.js for a while now and I just realized it can be blocking. I just cannot wrap my brain around the conditions under which Node.js becomes blocking.

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-02 11:55

    Let's get straight to the answers.

    1. Yes, Node.js is fully blocking, if you look at it like that. Let's say you read a huge, half a gigabyte CSV file from the database and try to json-encode it and send to the client (naive, I know, but bear with me).

    JSON encoding is basically string manipulation. That can be slow in a lot of languages, not just JavaScript. So if encoding such a json takes 20 seconds, you will load this CSV file (asynchronously), but then you'll spend 20 seconds parsing strings. During that time, nothing else can come in - not other callbacks, not other requests that you can send to the database/file system in the mean time - none of your actual programming runs except that single "JSON.stringify()" function.

    There are ways around this particular problem, but you should be aware of it - if your single function or a single statement like JSON.stringify takes a lot, it will block. You need to program your apps with that in mind.

    1. In essence, the event loop is sleeping until there are no tasks on its main queue. There can be other queues, like callback queues where you can be waiting on results of dozens of database operations. But they're not on your main event loop until they actually do get called back by the database reply.

    Say you're parsing that JSON from 1) above. And in the meantime you receive 5 new requests for that or other stuff. Your 5 requests go straight to the queue, and as one is finished, event loop checks for the next one to be processed. If there aren't any, it waits.

    1. The blocking does not render node useless. If it did, what would some other single-threaded languages that are not async like node do on large scales?

    Node is already used in large scale projects, I'm sure you can find many if you google a bit. The trick is to use the proper tools to proper solutions - as such, Node.js might require different strategies for dealing with CPU-intensive tasks or might even not be the right tool for the job.

提交回复
热议问题