NodeJs how to create a non-blocking computation

前端 未结 5 1701
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-10 01:53

I am trying to get my head around creating a non-blocking piece of heavy computation in nodejs. Take this example (stripped out of other stuff):

http.createS         


        
5条回答
  •  醉话见心
    2020-12-10 02:13

    This is a classic misunderstanding of how the event loop is working.

    This isn't something that is unique to node - if you have a long running computation in a browser, it will also block. The way to do this is to break the computation up into small chunks that yield execution to the event loop, allowing the JS environment to interleave with other competing calls, but there is only ever one thing happening at one time.

    The setImmediate demo may be instructive, which you can find here.

提交回复
热议问题