当文件被写入时如何处理readFile
这可能有点模糊,但我有一个节点的应用程序,写了一个小的计数器后,一堆文件:
Fs.readFile( Blender.LOG , function(error, data) { //read the log file if( error ) { throw error; } counter = parseInt( data ) + 1; //add this blend if(!isNaN( counter )) { //check if the number is a 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 it alone for now!'); } });
由于对应用程序的高度需求,我经常在readFile
的callback函数中得到一个isNaN
,因为这个文件已经被写入writeFile
的前一个实例中。
(有趣的是,虽然没有发生错误)
你将如何处理这个? 我真的不希望做这个封锁写保持应用程序的快速,但我也想也许排队写? 尽pipe如此,我仍然乐于接受。 🙂
谢谢
看起来你想要实现文件柜台,为了保证它是primefaces的,你需要这样做:
lock file read counter from file increment counter write counter to file unlock file
build议使用fs-ext模块,如果你想locking一个文件。
另一种方法:使用redis来存储计数器,它具有primefaces增量,性能更好,因为不需要经常写入磁盘。