0

When I use nodejs with cluster module, does following code mess up file contents?

if (!cluster.isMaster) {    
    var stream = fs.createWriteStream('samefile.txt', {flags: 'a'});
    for (var i = 0; i < 100000; ++i)
        stream.write('... big string ... \n');
}

I tested, but line was not mixed.

But I couldn't find document or base about file stream's cluster safety.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
chaeyk
  • 491
  • 1
  • 8
  • 20
  • 4
    Your following code looks totally clean. – Uwe Keim Feb 29 '16 at 06:45
  • 3
    What do you mean by "safe"? Writing to the same file from multiple processes without any central coordination will either have file locking or concurrency issues - one or the other. – jfriend00 Feb 29 '16 at 06:55
  • @jfriend00 maybe I asked question similar with http://stackoverflow.com/questions/1154446/is-file-append-atomic-in-unix/24270790#24270790 I want to know that "append mode" guarantee atomicity in nodejs file stream. – chaeyk Feb 29 '16 at 07:19
  • Is that other question the same thing you intend to ask here? – jfriend00 Feb 29 '16 at 16:23

0 Answers0