Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

node.js - Do file operations in NodeJS make use of OS async (polling?) or does it block the event loop?

Do file operations in NodeJS make use of OS async (polling?) or does it block the event loop? I want to create a webapp that requires a lot of uploading and I'm worried this may cause delays.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Polling is not used.

does it block the event loop?

It will if you use the sync versions. For example, using readFileSync will block other JS from running until the file is read and comes back. This could well be a problem if multiple parts of the script call such a method at different times.

But if you use the asynchronous versions (for example, readFile) which either accept a callback or return a Promise, they will not block other JS from running while the file operation is going on.

When in doubt, use one of the asynchronous versions - I'd only use the sync versions for smaller apps with segments of code that will only run once, such as for the initial reading of a config file, for which preventing other JS from running during that one-time delay isn't an issue.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...