javascript - Will Node.js get blocked when processing large file uploads? -
will node.js blocked when processing large file uploads?
since node.js has 1 thread, true when doing large file uploads other requests blocked?
if so, how should handle file uploads in nodejs?
all i/o operations handled node.js using multiple threads internally; it's programming interface i/o functionality that's single threaded, event-based, , asynchronous.
so big upload of example performed separate thread that's managed node.js, , when thread completes work, callback put onto event loop queue.
when cpu intensive task blocks. let's have task compute() needs run continuously, , cpu intensive calculations.
answer main question "how should handle file uploads in nodejs?"
check in code (or library) save file on server, dependent on
writefile() or writefilesync()?if using
writefile() asynchronous; if writefilesync() synchronous version. updates: in response comment:
"the answer "no, won't block" correct explanation wrong. js in 1 thread , i/o in 1 (same) thread. event loop / asynchronous processing / callbacks make possible. no multiple threads required. " - andrey-sidorov
there no async api file operations node.js uses thread pool that. can see in code of libuv. can @ source fs.readfile in lib/fs.js, you’ll see binding.read. whenever see binding in node’s core modules you’re looking @ portal land of c++. binding made available using node_set_method(target, "read", read). if know c, might think macro – originally, it’s function.
going async_call in read, 1 of arguments read: the syscall read. wait, doesn't function block?
yes, that’s not end of story. introduction libuv denotes following:
"the libuv filesystem operations different socket operations. socket operations use non-blocking operations provided operating system. filesystem operations use blocking functions internally, but invoke these functions in thread pool , notify watchers registered event loop when application interaction required."
summary: node api method writefile() asynchronous, doesn’t mean it’s non-blocking underneath. libuv book points out, socket (network) code non-blocking, filesystems more complicated. things event-based (kqueue), others use thread pool (as in case).
consider going through c code on node.js developed, more information:
Comments
Post a Comment