Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

node.js - javascript manage shared resource access for asynchronous functions

I'm writing a node.js server script that uses a shared text list data for multiple clients asynchronously.

the clients can read, add or update items of this shared list.

static getitems(){
    if (list== undefined) list = JSON.parse(fs.readFileSync("./list.json"));
    return list;
}

static additem(newitem){
    var key = Object.keys(newitem)[0];
    list[key] = newitem[key];
    fs.writeFileSync("./list.json", JSON.stringify(list));
}

clients can modify and get the list data using the following express APIs

app.get("/getlist"), (req, res)=>{
    res.send(TempMan.getTemplates());
});

app.post("/addlist"), (req, res)=>{
    additem(req.body.newitem)
    res.status(204).end()
});

with long background in C#, C++ and other desktop programming languages, although I red javascript doesn't run into race condition, I am so worried resource sharing is going to be a problem. I was first thinking of semaphores or shared lock or some other multiple thread management solutions in other languages, but yet read javascript doesn't need such methods.

does such node.js implementation run to resource sharing problem such as simultaneous attempts of file read/write? how can I solve this? do I need some kind of transaction functions I can use in javascript?

question from:https://stackoverflow.com/questions/65922141/javascript-manage-shared-resource-access-for-asynchronous-functions

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...