锁在物体上? [英] Locking on an object?

查看:68
本文介绍了锁在物体上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Node.js还是很陌生,我敢肯定对此有一个简单的答案,我只是找不到它:(

I'm very new to Node.js and I'm sure there's an easy answer to this, I just can't find it :(

我正在使用文件系统来保存程序包"(状态扩展名为"mypackage.idle"的文件夹),用户可以对这些程序执行操作,这些操作会使状态变为"qa"或正在部署"等等...如果服务器接受大量请求,并且同一包中有多个请求,我将如何检查状态,然后执行一个操作,该操作将更改状态,从而确保另一个请求在/之前没有更改它/在行动发生期间?

I'm using the filesystem to hold 'packages' (folders with a status extensions 'mypackage.idle') Users can perform actions on these which would cause the status to go to something like 'qa', or 'deploying' etc... If the server is accepting lots of requests and multiple requests come in for the same package how would I check the status and then perform an action, which would change the status, guaranteeing that another request didn't alter it before/during the action took place?

所以在C#中是这样的

lock (someLock) { checkStatus(); performAction(); }

谢谢:)

推荐答案

如果checkStatus()和performAction()是彼此依次调用的同步函数,则如前所述,它们的执行将不间断地运行直到完成. 但是,我怀疑实际上这两个函数都是异步的,组成它们的现实情况是这样的:

If checkStatus() and performAction() are synchronous functions called one after another, then as others mentioned earlier: their exectution will run uninterupted till completion. However, I suspect that in reality both of these functions are asynchoronous, and the realistic case of composing them is something like:

function checkStatus(callback){
  doSomeIOStuff(function(something){
    callback(something == ok);
  });
}

checkStatus(function(status){
  if(status == true){
    performAction();
  }
});

上面的代码受竞争条件的影响,因为当执行doSomeIOStuff而不是等待它时,可以满足新的请求.

The above code is subject to race conditions, as when doSomeIOStuff is being perfomed instead of waiting for it new request can be served.

您可能要检查 https://www.npmjs.com/package/rwlock 库.

这篇关于锁在物体上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆