考慮一個資料庫引擎,它在一個外部打開的檔案上運行——比如 SQLite,除了檔案句柄被傳遞給它的建構式。我正在為我的應用程式使用這樣的設定,但似乎無法弄清楚為什么 NodeJS 堅持在 2 秒的操作后關閉檔案描述符。我需要那個東西保持開放!
const db = await DB.open(await fs.promises.open('/path/to/db/file', 'r '));
...
(node:100840) Warning: Closing file descriptor 19 on garbage collection
(Use `node --trace-warnings ...` to show where the warning was created)
(node:100840) [DEP0137] DeprecationWarning: Closing a FileHandle object on garbage collection is deprecated. Please close FileHandle objects explicitly using FileHandle.prototype.close(). In the future, an error will be thrown if a file descriptor is closed during garbage collection.
該類DB在很長一段時間內廣泛使用提供的檔案描述符,因此關閉它相當煩人。在該類中,我使用諸如readFile,createReadStream()和readline模塊之類的方法來單步執行檔案的行。我正在傳遞{ autoClose: false, emitClose: false }給我正在使用的任何讀/寫流,但無濟于事。
- 為什么會這樣?
- 我怎樣才能阻止它?
謝謝
uj5u.com熱心網友回復:
我懷疑你在使用await這個時遇到了一個邪惡的問題
for await (const line of readline.createInterface({input: file.createReadStream({start: 0, autoClose: false})}))
如果您await在for回圈塊中使用其他任何地方(您是),則底層流會觸發其所有data事件并完成(當您在另一個時await,在某些情況下,您的行程甚至在您處理任何data或之前退出)line來自流的事件。這是一個真正有缺陷的設計,并且已經咬了很多其他人。
解決這個問題的最安全的方法是根本不使用asyncIterator,只需在 readline 物件的常規事件中自己包裝一個 promise。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/404745.html
標籤:
