I am using ASP.NET MVC 5 which gets data from Google Big Query. Due to the way Google Big Query is designed, I need to poll for results if job is not finished. Here is my code,
我正在使用ASP.NET MVC 5,它从Google Big Query获取数据。由于Google Big Query的设计方式,如果作业未完成,我需要轮询结果。这是我的代码,
var qr = new QueryRequest
{
Query = string.Format(myQuery, param1, param2)
};// all params are mine
var jobs = _bigqueryService.Jobs;
var response = await jobs.Query(qr, _settings.GoogleCloudServiceProjectId).ExecuteAsync();
var jobId = response.JobReference.JobId;
var isCompleted = response.JobComplete == true;
IList<TableRow> rows = response.Rows;
while (!isCompleted)
{
var r = await jobs.GetQueryResults(_settings.GoogleCloudServiceProjectId, jobId).ExecuteAsync();
isCompleted = r.JobComplete == true;
if (!isCompleted)
{
Thread.Sleep(100);
}
else
{
rows = r.Rows;
}
}
Looking at this code can someone tell me whether its good to call Thread.Sleep inside this context or I should continuously burn CPU cycles.
看看这段代码,有人可以告诉我在这个上下文中调用Thread.Sleep是否好,或者我应该不断地烧掉CPU周期。
1 个解决方案
#1
I wouldn't do this on the server side as one have to be careful which waiting calls to use to avoid high resource consumption under load.
我不会在服务器端执行此操作,因为必须小心哪些等待调用以避免负载下的高资源消耗。
Your users also don't get any feedback from the page. You can improve this situation by displaying a spinning wheel, but it might be better to show actual progress to the users.
您的用户也无法从该页面获得任何反馈。您可以通过显示旋转轮来改善这种情况,但最好向用户显示实际进度。
A better way of doing this will be AJAX calls to your web site. The call may return something like status, time elapsed and percentage complete (have a look at the BigTable's API). In this case you don't need to do any Thread.Sleep
or Task.Delay
kung fu.
更好的方法是对您的网站进行AJAX调用。调用可能会返回状态,已用时间和完成百分比等内容(请查看BigTable的API)。在这种情况下,您不需要执行任何Thread.Sleep或Task.Delay功夫。
Edit:
Oh, you already using AJAX! Just tear off any Thread.Sleep
and return result immediately to users. In browser, when AJAX call is completed update UI with information from the AJAX call. Job done.
编辑:哦,你已经在使用AJAX了!只需撕掉任何Thread.Sleep并立即将结果返回给用户。在浏览器中,当AJAX调用完成时,使用来自AJAX调用的信息更新UI。任务完成。
#1
I wouldn't do this on the server side as one have to be careful which waiting calls to use to avoid high resource consumption under load.
我不会在服务器端执行此操作,因为必须小心哪些等待调用以避免负载下的高资源消耗。
Your users also don't get any feedback from the page. You can improve this situation by displaying a spinning wheel, but it might be better to show actual progress to the users.
您的用户也无法从该页面获得任何反馈。您可以通过显示旋转轮来改善这种情况,但最好向用户显示实际进度。
A better way of doing this will be AJAX calls to your web site. The call may return something like status, time elapsed and percentage complete (have a look at the BigTable's API). In this case you don't need to do any Thread.Sleep
or Task.Delay
kung fu.
更好的方法是对您的网站进行AJAX调用。调用可能会返回状态,已用时间和完成百分比等内容(请查看BigTable的API)。在这种情况下,您不需要执行任何Thread.Sleep或Task.Delay功夫。
Edit:
Oh, you already using AJAX! Just tear off any Thread.Sleep
and return result immediately to users. In browser, when AJAX call is completed update UI with information from the AJAX call. Job done.
编辑:哦,你已经在使用AJAX了!只需撕掉任何Thread.Sleep并立即将结果返回给用户。在浏览器中,当AJAX调用完成时,使用来自AJAX调用的信息更新UI。任务完成。