我正在一个有文件处理的项目工作。文件来自另一个应用程序,并且数量很大。现在我想为其他应用程序锁定该文件夹,直到扫描它的文件和进程。处理后,所有文件将删除并解锁此文件夹。我已经用谷歌搜索了它,但没有找到解决方案。有没有使用节点.js的方法?请指教。
对于我的文件系统同步,我倾向于使用锁定文件模块。它允许函数锁定文件/文件夹,在释放文件/文件夹之前阻止访问(实际上是互斥锁)。您可以在他们的 npm 页面上找到您尝试执行的操作的示例:
var lockFile = require('lockfile');
lockFile.lock('some-file.lock', opts, function (er) {
// if the er happens, then it failed to acquire a lock.
// if there was not an error, then the file was created,
// and won't be deleted until we unlock it.
// do my stuff, free of interruptions
// then, some time later, do:
lockFile.unlock('some-file.lock', function (er) {
// er means that an error happened, and is probably bad.
})
});
其中"some-file.lock"是你尝试锁定的文件/目录。