I cannot seem to get a straight answer on this. I am using the NPM package 'worker-farm'
import workerFarm from 'worker-farm';
to provide me with concurrency - it is working very well. In one part of waht I am doing, I need to upload a file to some blob storage of cloud service provider. I only want to do this once. So I do the following using 'async-mutex'
// This is being run on six separate workers.
const mutex = new Mutex();
let panoResolutionSet = new Set<Resolution>();
// ...
mutex
.runExclusive(async () => {
if (!panoResolutionSet.has(resolution)) {
const panoFilePath = `${resolutionPath}/pano_${faceGenerator.imageInfo.width}.jpg`;
logger?.info(`Attempting to write the FULL pano "${faceGenerator.imageInfo.width}" for ${Resolution[resolution]}`);
const succeeded = await azureStorageService.upload(
new AzureBlobDetails(panoFilePath, this.toArrayBuffer(faceGenerator.imageData)),
false
);
if (succeeded) {
panoResolutionSet.add(resolution);
logger?.info(`Panorama for ${Resolution[resolution]} upload to "${panoFilePath}" successfully`);
}
}
});
but when debugging in VSCode, this code block is entered at the same time (perhaps the debugger is affecting the state?). The question is simply:
Q. Can I use this async-mutex to provide thread safety when using multiple workers?