[英]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.