请求互斥记录
我正在使用 C# 开发两种类型的应用程序。一种是 Web 服务,一种是 MVC3 应用程序。
我两者都有同样的问题。我正在尝试创建一种方法,当请求时,该方法返回要处理的分布式节点的记录。
并发问题导致这很危险。一旦进程“A”请求要处理的记录。如果进程“B”在数据库检查“A”的记录之前请求一个记录,则它们可能都会获得相同的记录,这对系统不利。
在网络服务中,我有一个名为“GetRecordToProcess”的网络方法。控制器中也有一个相应的 MVC3 ActionResult 执行相同的操作。
注意 Web 服务和 MVC 操作不需要相互排斥。他们实际上在两个不同的表上工作,但我碰巧在两个不同的项目上遇到了同样的问题。
我有一个使用 Mutex 的工作版本,但是当我调用 Mutex.WaitOne 时,进程的 CPU 利用率几乎达到 60%,这真是太糟糕了。有什么简单的方法来做到这一点的建议吗?这似乎很明显,这将是一个常见的分布式计算需求,我只是不知道常见的解决方案。
I'm working with C# on two types of applications. One is a web service one is a MVC3 application.
I have the same problem with both. I am trying to create a method that when requested, returns a record for a distributed node to work on.
Concurrency issues cause this to be dangerous. Once process "A" requests a record to work on. If process "B" requests one before the database checks the record for "A" out, they might both get the same record which is bad for this system.
In the webservice I have a web method that is being called "GetRecordToProcess". There is also a corresponding MVC3 ActionResult in a controller that does the same thing.
NOTE the web service and MVC Action DONT need to be mutually exclusive to eachother. They are actually working on two different tables, but I happened to run into the same issue on two separate projects.
I have a working version of this with Mutex but the processes are running my CPU up to almost 60% when i call Mutex.WaitOne which is baaaad. Any suggestions of a light way to do this? This seems obvious that it would be a common distributed computing requirement, I just don't know the common solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
后端需要负责解决并发问题。例如,您可以创建一个
CheckOut
表来跟踪当前签出记录的人员。增强您的 Web 服务,使其具有返回记录的只读版本的
GetRecordToRead()
和尝试签出记录的TryGetRecordToWrite()
(通过添加新的签出记录到 CheckOut 表),如果成功则返回该记录的可写版本The back-end needs to be responsible for resolving the concurrency issues. For example, you could create a
CheckOut
table that keeps track of who has the record currently checked out.Enhance your web service to have a
GetRecordToRead()
which returns a read-only version of the record, andTryGetRecordToWrite()
which attempts to check out the record (by adding a new check-out record to the CheckOut table) and if it succeeds returns a writeable version of the record