- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如果多个独立的 Node 进程使用 fs.appendFile()
将文本 block (每个大于 4KB)附加到单个文件,我是否可以在没有文件锁定的情况下丢失/损坏数据scheme 还是 fs.appendFile
是一个原子操作?
我最关心的是 Linux 和本地文件系统。
最佳答案
答案可能取决于操作系统和/或文件系统,但您可以使用它来测试:
// This script aims to test/prove that you can append to a single file from
// multiple processes with buffers up to a certain size, without causing one
// process' output to corrupt the other's.
//
// The script takes one parameter, the length of the buffer. It then creates
// 20 worker processes which each write 50 lines of the specified buffer
// size to the same file. When all processes are done outputting, it tests
// the output file to ensure it is in the correct format.
const child_process = require('child_process');
const fs = require('fs');
const NUM_WORKERS=20;
const LINES_PER_WORKER=50;
const OUTPUT_FILE='./foo';
// each worker will output $LINES_PER_WORKER lines to the output file
function run_worker(worker_num, buf_len) {
// Each line will be a specific character, multiplied by the line length.
// The character changes based on the worker number.
const filler_len=buf_len-1; // -1 -> leave room for \n
const filler_char=String.fromCharCode(+worker_num+64);
line=filler_char.repeat(filler_len) + '\n';
for (let i=0; i<LINES_PER_WORKER; i++) {
fs.appendFile(OUTPUT_FILE, line, (e) => {
if (e) {
console.log('Oh noes!', e);
throw e;
}
});
}
}
if (process.argv[2] === "worker") {
run_worker(process.argv[3], process.argv[4]);
} else {
const buf_len=+process.argv[2] || 4096;
try {
fs.unlinkSync(OUTPUT_FILE);
} catch (e) {
// swallow file if it doesn't exist (the script was never run before)
if (e.code !== 'ENOENT') {
throw e;
}
}
console.log(`Launching ${NUM_WORKERS} worker processes`);
let finished = 0;
for (let i=1; i <= NUM_WORKERS; i++) {
let proc = child_process.spawn(process.execPath, [process.argv[1], 'worker', i, buf_len], {stdio: 'inherit'});
proc.on('close', (code) => {
if (code) {
console.log(`Worker exited with code ${code}`);
process.exit(code);
}
finished++;
if (finished === NUM_WORKERS) {
allFinished();
}
});
proc.on('error', (e) => {
console.log('Worker errored!');
throw e;
});
}
console.log(`Each line will be ${buf_len} characters long`);
console.log(`Waiting for processes to exit`);
// Now we want to test the output file. Each line should be the same letter
// repeated buf_len-1 times (remember the \n takes up one byte). If we had
// workers writing over eachother's lines, then there will be mixed characters
// and/or longer/shorter lines.
function allFinished() {
console.log(`Testing output file`);
// Make sure the file is the right size (ensures processes didn't write over
// each other's lines)
const expected_file_size=NUM_WORKERS * LINES_PER_WORKER * buf_len;
const actual_file_size=fs.statSync(OUTPUT_FILE).size;
if ( expected_file_size !== actual_file_size) {
console.log(`Expected file size of ${expected_file_size}, but got ${actual_file_size}`);
process.exit(1)
}
// File size is OK, test the actual content
// Scan line by line
// Note: Doesn't work on cygwin for lines < 255
const line_length=buf_len-1;
const lineReader = require('readline').createInterface({
input: require('fs').createReadStream(OUTPUT_FILE)
});
let num_lines = 0;
lineReader.on('line', function (line) {
const first_char = line[0];
if (line !== first_char.repeat(line_length)) {
num_lines++
}
});
if (num_lines > 0) {
console.log("Found $num_lines instances of corrupted lines");
} else {
console.log(`All's good! The output file had no corrupted lines.`);
}
fs.unlinkSync(OUTPUT_FILE);
}
}
两件事:
我将此脚本从 bash 脚本移植到 Node.js https://www.notthewizard.com/2014/06/17/are-files-appends-really-atomic/ .
在 macOS Mojave 上,我无法导致交错输出。
关于javascript - Node `fs.appendFile` 是原子的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50435287/
我可以使用下面的函数来覆盖一个文本文件: let writeFile ~filename:fn s = let oc = open_out fn in output_string oc s;
以下代码的作用是从一个文件夹中取出几个文本文件并将它们附加到一个文件中: #!/usr/bin/env node 'use strict' const fs = require('fs') ,
fs.appendFile() 的文档对于原子性保证含糊不清。 我特别想知道,如果在同一个文件上对数据进行两次或多次调用,而不等待其间的回调,是否可以交错或删除数据(例如,由于写入重叠区域)。例如,采
我是nodejs新手,我正在尝试附加功能。当我运行下面的代码时,只需在提示用户后输入有关最喜欢的歌手的信息,然后将其放入包含歌手姓名的文件中。并附加最喜欢的歌曲,似乎如果用户输入退出,则应该将其附加到
我刚开始使用 Nodejs 编写聊天日志机器人。现在效果很好。除了几个小时之后。机器人会为每个用户创建一个文档,如果该用户尚不存在这样的文档,则该用户将键入一些内容;如果存在,则它将把它放入该文件中。
如果多个独立的 Node 进程使用 fs.appendFile() 将文本 block (每个大于 4KB)附加到单个文件,我是否可以在没有文件锁定的情况下丢失/损坏数据scheme 还是 fs.ap
应用程序必须将日志数据存储到网络驱动器。当驱动器托管在 Windows 下时,一切正常。当驱动器托管在 Mac 下时,读/写是可以的,但是 append 获取 EPERM,所以 log 根本不起作用。
console.log 正确返回 req.query (request.query),如 { name: 'sean', comments: 'Hey' }。但是当我尝试使用 fs.appendFil
我在尝试使用 Zend 的 headScript()->appendFile('file name') 附加 javascript 文件时遇到问题。我的布局设置如下: headScript()
http://nodejs.org/api/fs.html#fs_fs_appendfile_filename_data_options_callback fs.appendFile 是否保持打开文件
我正在尝试将数据附加到某些文件中。 文档说fs.appendFile: Asynchronously append data to a file, creating the file if it no
我一直在尝试创建一个程序来保存 Slack 团队的日志。我已经完成了大部分工作,但到目前为止无法成功使用 fs.appendFile() ,因为文档声称如果文件不存在,它将创建该文件(并且不要事先使用
我有一个应用程序,它在运行时在其工作目录中创建一些 CSV 文件。该应用程序在我的 docker 容器之外运行良好,但是当我在其中运行它时,出现错误: Error: ENOENT: no such f
我在创建与 Node 并行的进程时遇到了问题,同时在完成简单的 HTTP GET 请求后退出。我注意到,如果我在appendFile 的回调中触发process.exit(),某些文件将不会在 Nod
查看 fs 文档,我正在寻找一个可以与 fs.appendFile 一起使用的标志,如果路径不存在,则会引发错误。 如果路径已经存在,我会看到与引发错误有关的标志,但如果路径不存在,我没有看到会引发错
我有一个包含大约 120k HTML 页面的文件夹,我需要打开这些页面(每个文件大约 70kb),使用 xPath 解析一些数据并将该数据附加到 .csv 文件。 下面是我的代码: 它应该从 pars
我正在开发将使用 开始的 node.js 应用程序 for(i=0; i
我是一名优秀的程序员,十分优秀!