Consumer/Producer with BlockingCollection appears slow

雨燕双飞 提交于 2019-12-25 05:07:21

问题


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. 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.

The problem is that I have read that BlockingCollection is the preferred approach for this, BUT is appears very slow.

On average 150ms between when I get the external data and when I call ProcessDataOnGrid. Am I using this incorrectly, or is there a better way to wait for data BUT only for a fixed period of time?

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;
    }
}

回答1:


BlockingCollection is NOT slow. I had another thread competing for the same resource.

Many thanks.



来源:https://stackoverflow.com/questions/24711125/consumer-producer-with-blockingcollection-appears-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!