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. 后端開發 - mysql按時間分段統計的sql語句怎么寫好?2. python3.x - python lxml無法查找第一個tag有鏈接和沒有內容tag的子tag的text?3. mysql - 在下剛入門sql 關于sql的語法詢問4. 初來乍到,相對路徑問題,新手求教5. 老師您好!我有一個問題、6. javascript - iframe 為什么加載網頁的時候滾動條這樣顯示?7. c++ - 如何在python的阻塞的函數中獲取變量值8. javascript - 請問為啥這個不能渲染成功?9. javascript - 根據不同數據顯示不同內容10. 哭遼 求大佬解答 控制器的join方法怎么轉模型方法
