Consumer/Producer with BlockingCollection appears

2019-08-31 02:48发布

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条回答
ゆ 、 Hurt°
2楼-- · 2019-08-31 03:51

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

Many thanks.

查看更多
登录 后发表回答