gpt4 book ai didi

node.js - 一次修改一个文件

转载 作者:行者123 更新时间:2023-12-04 17:08:10 27 4
gpt4 key购买 nike

我正在使用 JSON 文件作为数据库,因此我需要在文件尚未完全更新时消除 read/write 错误。

如何从同一文件创建更新进程队列?

(我正在使用 typescript )

最佳答案

你可以使用bull,它是一个经过良好测试的第三方队列库。你可以使用 npm 安装它:

npm install bull --save

要添加类型运行这个:

npm install @types/bull --save-dev

您可以按照以下步骤操作:

const Queue = require('bull');

const videoQueue = new Queue('video transcoding', 'redis://127.0.0.1:6379');
const audioQueue = new Queue('audio transcoding', { redis: { port: 6379, host: '127.0.0.1', password: 'foobared' } }); // Specify Redis connection using object
const imageQueue = new Queue('image transcoding');
const pdfQueue = new Queue('pdf transcoding');

videoQueue.process(function (job, done) {

// job.data contains the custom data passed when the job was created
// job.id contains id of this job.

// transcode video asynchronously and report progress
job.progress(42);

// call done when finished
done();

// or give a error if error
done(new Error('error transcoding'));

// or pass it a result
done(null, { framerate: 29.5 /* etc... */ });

// If the job throws an unhandled exception it is also handled correctly
throw new Error('some unexpected error');
});

audioQueue.process(function (job, done) {
// transcode audio asynchronously and report progress
job.progress(42);

// call done when finished
done();

// or give a error if error
done(new Error('error transcoding'));

// or pass it a result
done(null, { samplerate: 48000 /* etc... */ });

// If the job throws an unhandled exception it is also handled correctly
throw new Error('some unexpected error');
});

imageQueue.process(function (job, done) {
// transcode image asynchronously and report progress
job.progress(42);

// call done when finished
done();

// or give a error if error
done(new Error('error transcoding'));

// or pass it a result
done(null, { width: 1280, height: 720 /* etc... */ });

// If the job throws an unhandled exception it is also handled correctly
throw new Error('some unexpected error');
});

pdfQueue.process(function (job) {
// Processors can also return promises instead of using the done callback
return pdfAsyncProcessor();
});

videoQueue.add({ video: 'http://example.com/video1.mov' });
audioQueue.add({ audio: 'http://example.com/audio1.mp3' });
imageQueue.add({ image: 'http://example.com/image1.tiff' });

可以查看bull的文档实现它。

注意:如果您不了解如何实现它的要点,我建议您关注 this链接。

关于node.js - 一次修改一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70056875/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com