C#信号灯无法正常工作,无法弄清原因 [英] C# Semaphores not working as expected, cannot figure out why

查看:38
本文介绍了C#信号灯无法正常工作,无法弄清原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#构建ASP.NET(.NET Framework)应用程序,正在对称为"LinuxDocker"的后端服务进行API调用,并且试图限制从ASP.NET对其进行的12个并发调用的数量应用.这是我写的代码:

I am building an ASP.NET (.NET Framework) application in C# I am making API calls to a backend service called "LinuxDocker" and I am trying to limit the number of 12 concurrent calls to it from the ASP.NET application. Here is the code I wrote:

private static Semaphore LinuxDockerSemaphore = new Semaphore(12, 12);

public static SemaphoreWaiter WaitForLinuxDocker(int timeoutMS = -1)
{
      return new SemaphoreWaiter(LinuxDockerSemaphore, timeoutMS);
}

public class SemaphoreWaiter : IDisposable
{
    Semaphore Slim;

    public SemaphoreWaiter(Semaphore slim, int timeoutMS = -1)
    {
        Slim = slim;
        Slim.WaitOne(timeoutMS);
    }

    public void Dispose()
    {
        Slim.Release();
    }
}

然后,当我调用后端服务时,我会像这样:

Then when I call my backend service, I do so like this:

using (ConcurrencyManager.WaitForLinuxDocker())
{
      // Call backend service here
}

所以看来这应该限制为12个并发调用,但是当我从具有100个并发调用的集成测试中对其进行测试时,它基本上一次只允许1个请求通过,一次不允许12个调用通过.

So it seems like this should limit to 12 concurrent calls, however when I test it from an integration test with 100 simultaneous calls, it basically only allows 1 request at a time to go through, not 12 calls at a time to go through.

该服务正在Windows Server 2016和.NET Framework 4.7的IIS上运行.

The service is running on IIS on Windows Server 2016 and .NET Framework 4.7.

我已经多次阅读此代码,无法弄清为什么它不起作用.

I have read this code many times and can't figure out why it doesn't work.

任何帮助将不胜感激.

推荐答案

可能您的后端由多个 w3wp.exe 工作进程提供服务.因为您的信号灯是没有名称的,所以它是本地"的.信号量(对于每个过程而言是局部的)而不是全局"信号量.信号量(系统范围内):

Possibly your backend is being served up by multiple w3wp.exe worker processes. Because your semaphore is created without a name it is a "local" semaphore (local to each process) as opposed to a "global" semaphore (system-wide):

信号量有两种类型:局部信号量和命名系统信号量.如果使用接受名称的构造函数创建Semaphore对象,则该对象将与该名称的操作系统信号灯关联.命名的系统信号在整个操作系统中都是可见的,可用于同步进程的活动.您可以创建代表相同命名系统信号的多个信号对象,并可以使用OpenExisting方法打开现有的命名系统信号.

Semaphores are of two types: local semaphores and named system semaphores. If you create a Semaphore object using a constructor that accepts a name, it is associated with an operating-system semaphore of that name. Named system semaphores are visible throughout the operating system, and can be used to synchronize the activities of processes. You can create multiple Semaphore objects that represent the same named system semaphore, and you can use the OpenExisting method to open an existing named system semaphore.

仅在您的进程内存在本地信号灯.进程中任何引用了本地Semaphore对象的线程都可以使用它.每个信号量对象都是一个单独的本地信号量.

A local semaphore exists only within your process. It can be used by any thread in your process that has a reference to the local Semaphore object. Each Semaphore object is a separate local semaphore.

REF:信号量类

这篇关于C#信号灯无法正常工作,无法弄清原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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