node.js - Writing multiple files asynchronously -
while trying understand event loop, wrote quick snippet test assumptions. no surprise, wrong!
import fs 'fs' let buffera = new buffer(1e+9) buffera.fill(0) let bufferb = new buffer(1e+0) bufferb.fill(0) let filea = fs.opensync('filea', 'w') let fileb = fs.opensync('fileb', 'w') fs.write(filea, buffera, 0, buffera.length, (err) => { console.log(err || 'wrotea')}) console.log('started writing a..') fs.write(fileb, bufferb, 0, bufferb.length, (err) => { console.log(err || 'wroteb')}) console.log('started writing b..')
i hoping both files written asynchronously (i.e., fileb finishes first), output follows:
started writing a.. started writing b.. wrotea wroteb
with delay before wrotea
shown. while fs.write
seems operate asynchronously code (i.e., logs written first) seems can have 1 file writing @ time?
per ben noordhuis on github issue opened this:
libuv serializes disk writes on os x due nasty quirk documented here. it's possible newer os x releases fare better hasn't been tested.
node.js uses libuv things file i/o. so...it's serialized (crucially) not block event loop. filea may block writing fileb, not other js stuff. , on os x.
Comments
Post a Comment