简体   繁体   English

带有BlockingCollection的消费者/生产者看起来很慢

[英]Consumer/Producer with BlockingCollection appears slow

I am getting data from an external socket connection through the "Producer" below. 我正在通过下面的“生产者”从外部套接字连接获取数据。

I place the data into a BlockingCollection , which is then read by the consumer. 我将数据放入BlockingCollection ,然后由使用者读取。 If the consumer does NOT receive data within a fixed period, it fires off anyway, such that my ProcessDataOnGrid , does something whenever data arrives OR AT LEAST after x millisecs. 如果使用者在固定时间内没有收到数据,则无论如何都会触发,这样我的ProcessDataOnGrid就会在数据到达x毫秒后至少到达时执行某些操作。

The problem is that I have read that BlockingCollection is the preferred approach for this, BUT is appears very slow. 问题是我已经阅读过BlockingCollection是实现此目的的首选方法,但是BUT的显示速度非常慢。

On average 150ms between when I get the external data and when I call ProcessDataOnGrid . 从获取外部数据到调用ProcessDataOnGrid平均间隔为ProcessDataOnGrid Am I using this incorrectly, or is there a better way to wait for data BUT only for a fixed period of time? 我是否使用不正确,还是有更好的方法仅在固定时间段内等待数据BUT?

public BlockingCollection<TickRecord> trPipe = new BlockingCollection<TickRecord>();

Producer: 制片人:

public void ProcessMarketData(string key, string intraMessage)
{
    //////////
    //   Gets External data from intraMessage
    ////////////
    try
    {
        if (GRID!=null)
        {
            TickRecord tr = new TickRecord(intraMessage);

            while ( ! AddToFeedPipe(key, tr) )
            {
                Thread.Sleep(1000);
            }
        }
    }
    catch (Exception e)
    {
    }
  }
}

public bool AddToFeedPipe(string key, TickRecord tr)
{
        try
        {
            foreach (var s in cReader.STREAMS)
            {
                if (s.key == key)
                {
                    s.trPipe.Add(tr);
                    return true;
                }
            }

            return false;
        }
        catch (Exception)
        {
            return false;
        }
}

Consumer: 消费者:

public void Read()
{
    DateTime DTNOW = DateTime.UtcNow;

    TimeSpan gridNextTS = G.gridNextDT.Subtract(DTNOW);

    try
    {
        if (trPipe.TryTake(out tr,gridNextTS) == false)
        {
            tr = trGAP;
        }
        else if (tr == null)
        {
            EOF = true;
            return;
        }

        ProcessDataOnGrid(tr);
    }
    catch (Exception e)
    {
        tr = null;
        EOF = true;
        return;
    }
}

BlockingCollection is NOT slow. BlockingCollection并不慢。 I had another thread competing for the same resource. 我有另一个线程在争用同一资源。

Many thanks. 非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 生产者/消费者,BlockingCollection和等待更改 - Producer/Consumer, BlockingCollection, and waiting for changes 在BlockingCollection生产者消费者中完成与IDisposable的比较 - Finalize vs. IDisposable in BlockingCollection Producer Consumer 如何使用BlockingCollection <>解决生产者/消费者竞争条件 - How to solve producer/consumer race condition with BlockingCollection<> 生产者消费者有单独的类,具有共同的BlockingCollection - Producer Consumer Separate Classes with common BlockingCollection 无需BlockingCollection的简单生产者-消费者集合 - Simple producer-consumer collection without BlockingCollection 如何实现BlockingCollection来修复此生产者/消费者问题? - How to implement BlockingCollection to fix this Producer/Consumer issue? C#BlockingCollection生成者使用者而不阻塞使用者线程 - C# BlockingCollection producer consumer without blocking consumer thread 使用阻塞收集和任务的经典生产者消费者模式 .net 4 TPL - classic producer consumer pattern using blockingcollection and tasks .net 4 TPL 使用 BlockingCollection 通过生产者-消费者模式保存图像 - Saving images via producer-consumer pattern using BlockingCollection 多线程多生产者和消费者线程将不会同步BlockingCollection竞争条件 - Multithreading Multiple Producer and Consumer Threads Won't Sync BlockingCollection Race Condition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM