Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libuv uses blocking file system calls internally – Why? How?

I just learned that Node.js crown jewel libuv uses blocking system calls for file operations. The asynchronous behavior is implemented with threads! That raises two questions (I only care about Unix):

  1. Why is it not using the non-blocking filesystem calls like it does for networking?
  2. If there are one million outstanding file reads, it probably does not launch one million threads... What does libuv do??
like image 764
Robert Siemer Avatar asked Nov 17 '13 14:11

Robert Siemer


2 Answers

  1. Most likely to support synchronous operations such as fs.renameSync() vs fs.rename().

  2. It uses a thread pool, as explained in the "Note" at the link you provded.

    [...] but invoke these functions in a thread pool and notify watchers registered with the event loop when application interaction is required.

    So, it creates a limited number of threads and reuses them as they become available.

Also, regarding the quip of "crown jewel:" Node.js and libuv aren't magic. They're good tools to have at your disposal, but certainly have their limitations.

Though, the hyperbole of "one million file reads" would be a stretch for any platform to manage without constraint.

like image 139
Jonathan Lonowski Avatar answered Sep 18 '22 15:09

Jonathan Lonowski


  1. The same non-blocking API can not be used, as O_NONBLOCK and friends don’t work on regular files! For Linux AIO is available, but it has it’s own quirks (i.e. depends on the filesystem, turns silently blocking for some operations).

  2. I have no idea.

like image 30
Robert Siemer Avatar answered Sep 17 '22 15:09

Robert Siemer