node.js - How to deal with readFile when file is currently written to -
this might bit fuzzy have node app writes small counter file after doing bunch of stuff:
fs.readfile( blender.log , function(error, data) { //read log file if( error ) { throw error; } counter = parseint( data ) + 1; //add blend if(!isnan( counter )) { //check if number number fs.writefile( blender.log, counter, function(error) { if( error ) { throw error; } blender.debugging( 'counter: added', 'report' ); }); } else { //throw error blender.log.error(' counter number not valid ("' + counter + '"). leaving alone now!'); } });
due high demand on app more not isnan
in callback of readfile
because file open write writefile
previous instance.
(it interesting me there no errors thrown though)
how deal this? don't want make blocking write keep app fast perhaps queue write? open though. :)
thanks
it looks want implemention file counter, makesure it'is atomic, need this:
lock file read counter file increment counter write counter file unlock file
suggest use fs-ext module, if want lock file.
another way: use redis store counter, have atomic increment, , it's performance better because without need write disk frequently.
Comments
Post a Comment