node fs.writeFileSync() never returns
我正在创建管道并尝试对其进行写入。但是写作永远都行不通。
1 2 3 4 5 6 7 8 | import * as fs from 'fs'; import * as mkfifo from 'mkfifo'; mkfifo.mkfifoSync('/tmp/my_fifo', 0o600); fs.writeFileSync('/tmp/my_fifo', 'Hey there!'); console.log('here'); |
如果我评论除
但是,
即使使用回调版本`fs.writeFile()1,也不会触发回调。
1 2 3 4 5 6 7 8 9 | fs.writeFile('/tmp/my_fifo', 'Hey there!', (err) => { if (err) { return console.log('Write error: ' + err); } console.log('Write success'); process.exit(); }); |
从不调用回调。
我在做什么错?
仅作为检查,我打开并尝试从另一个脚本读取管道:
1 2 3 4 5 6 7 | fs.readFile('/tmp/my_fifo', (err, data) => { if (err) { return console.log('Read error: ' + err); } console.log('Read success. Data is: ' + data); }); |
在编写者端,我看到错误:
1 | Error: ESPIPE: invalid seek, write |
在读者那边,我看到:
1 | Read success. Data is: |
空数据,但读取成功。
更新
如果我根本不通过
所以我的问题是如何写入此管道?
您必须使用
附加到命名管道
1 2 3 4 5 | const fs = require("fs"); const mkfifo = require("mkfifo"); mkfifo.mkfifoSync("/tmp/my_fifo", 0o600); fs.appendFileSync("/tmp/my_fifo","hello world", ...); |
从命名管道读取
1 2 | const fs = require("fs"); console.log(fs.readFileSync("/tmp/my_fifo").toString()); |