我需要一些帮助ConcurrentQueue和BlockingCollection。返回调用方法 - BlockingCollection队列项目
该方案是即时尝试调节请求并符合每秒1个请求的限制,当我从队列中取出一个项目时会发生调节。该应用程序是一个MVC 4应用程序,因此在任何时候都有可能有多个生产者,并且我只接触到一个消费者/网络服务。
- 生产者
GetUser(string url)
将请求添加到队列中, 请求只是一个网址。 - 通过执行一些检查以确保它不违反限制来处理BlockingCollection中的第一项。
- 从消费者处下载回复
- 然后以某种方式将下载响应返回给调用方法。 ThrottledDownload
总之,我想处理队列中的项目,下载响应并将其发送回调用方法。将它发送回调用方法是im卡住的地方。我在这里有什么选择?
//I want to do something like this, and wait for the throttled response to return
public class WebService()
{
public string GetUser(string name)
{
var url = buildUrl(name);
var response = string.Empty;
var downloadTask = Task.Factory.StartNew(() => {
response = WebServiceHelper.ThrottledDownload(url);
});
downloadTask.Wait();
return response;
}
}
public static class WebServiceHelper()
{
private static BlockingCollection<Request> requests = new BlockingCollection<Request>();
static WebServiceHelper()
{
foreach(var item in requests.GetEnumerableConsumer()) {
string response = DoWork(item.Url);
//How can i send this back to the calling method?
}
}
public static string ThrottledDownload(string url)
{
//Add the request to the blocking queue
requests.Add(new Request(url, someId));
//How do i get the result of the DoWork method?
}
}
您是否有手动创建工作人员的原因,而不仅仅是使用线程池?通过它的声音,你可以在调用Task.Run的时候完成工作,在这种情况下,你可以通过'Task'上的'ContinueWith'调用来处理结果。 – Servy 2013-03-26 17:56:13
@Servy我需要依次执行工作(Http请求到Web服务),并符合每秒1个请求规则。 Tbh我没有看过ThreadPool,因为我认为线程安全队列在阅读完毕后会是理想的。在实践中,虽然我不知道如何将结果返回到多生产者单一消费者场景中的调用过程 – gdp 2013-03-26 18:02:18
请详细说明(编辑您的问题以提供更多信息)“返回结果”在此表示的含义。我不清楚哪些请求来自哪里,以及在工作完成时要通知哪个进程或线程。 – 2013-03-26 18:56:09