node.js - 來幫我捋一下node中fs模塊watch實現原理
問題描述
來探索一下node中fs模塊watch實現原理,
const fs = require(’fs’);var fileName = ’a.txt’;fs.watch(fileName, (function () { var count = 0; return function () {count++;console.log('文件' + fileName + ' 內容剛剛改變。。第' + count + '次'); };})());console.log('watching file...');
fs如何實現針對文件變化做出響應的事件通知的呢?如果說是通過監聽文件大小變化,或者其他?
下面是通過node源碼看到的一些東西:
const FSEvent = process.binding(’fs_event_wrap’).FSEvent;function FSWatcher() { EventEmitter.call(this); var self = this; this._handle = new FSEvent(); this._handle.owner = this; this._handle.onchange = function(status, eventType, filename) { if (status < 0) { self._handle.close(); const error = !filename ? errnoException(status, ’Error watching file for changes:’) : errnoException(status, `Error watching file ${filename} for changes:`); error.filename = filename; self.emit(’error’, error); } else { self.emit(’change’, eventType, filename); } };}
后面的fs_event_wrap.cc 基本都是外星語言了。
下面我再docker當中掛載的數據卷監聽不到事件通知
問題解答
回答1:看一下這個吧 https://github.com/nodejs/nod...
回答2:快來了Boy解答一下啊,不知道踩我的,腦殼有坑?
相關文章:
1. windows誤人子弟啊2. mysql優化 - MySQL如何為配置表建立索引?3. 實現bing搜索工具urlAPI提交4. 關于mysql聯合查詢一對多的顯示結果問題5. 數據庫 - Mysql的存儲過程真的是個坑!求助下面的存儲過程哪里錯啦,實在是找不到哪里的問題了。6. 我在網址中輸入localhost/abc.php顯示的是not found是為什么呢?7. 如何用筆記本上的apache做微信開發的服務器8. python - linux怎么在每天的凌晨2點執行一次這個log.py文件9. MySQL主鍵沖突時的更新操作和替換操作在功能上有什么差別(如圖)10. 冒昧問一下,我這php代碼哪里出錯了???
