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. java-se - 正在學習Java SE,為什么感覺學習Java就是在學習一些API。2. html5 - iOS的webview加載出來的H5網頁,怎么修改html標簽select的樣式字體?3. java - 安卓電視盒子取得了root權限但是不能安裝第三方應用,請問該怎么辦?4. 一個mysql聯表查詢的問題5. 運行python程序時出現“應用程序發生異常”的內存錯誤?6. python - 如何使用pykafka consumer進行數據處理并保存?7. android - 離線地圖的這種列表該怎么實現?8. 主從備份 - 跪求mysql 高可用主從方案9. javascript - git clone 下來的項目 想在本地運行 npm run install 報錯10. mysql - 一個表和多個表是多對多的關系,該怎么設計
