我目前正在开发一个项目,我需要排队一些工作进行处理,这是要求:
所以我想要类似于:
Task<result> QueueJob(params here)
{
/// Queue the job and somehow return a waitable task that will wait until the queued job has been executed and return the result.
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过后台运行任务,只是将项目从队列中拉出来并处理作业,但难度是从后台任务到方法.
如果需要的话,我可以在QueueJob方法中使用刚刚请求完成回调的路径,但是如果我能得到一个透明的任务可以让你等待处理工作(即使有工作)也会很棒在它排队之前).
我有以下异步代码从我的项目中的很多地方调用:
public async Task<HttpResponseMessage> MakeRequestAsync(HttpRequestMessage request)
{
var client = new HttpClient();
return await client.SendAsync(request).ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud)
如何调用上述方法的示例:
var tasks = items.Select(async i =>
{
var response = await MakeRequestAsync(i.Url);
//do something with response
});
Run Code Online (Sandbox Code Playgroud)
我正在击中的ZenDesk API允许每分钟大约200个请求,之后我收到了429错误.如果我遇到429错误,我需要做一些Thread.sleep,但是使用async/await,并行线程中可能会有很多请求等待处理,我不知道怎么能让所有人都睡不着持续5秒左右,然后再次恢复.
解决这个问题的正确方法是什么?我想听听快速解决方案以及良好的设计解决方案.