【发布时间】:2013-11-15 14:41:28
【问题描述】:
我对异步 C# 编码非常陌生,对于 Azure 表,有同步 + 异步调用可供选择。假设 1)我必须在我的方法中获取一个实体,并且 2)线程在此期间没有其他可以做的事情,是否有任何理由使用下面的异步版本的代码?通过释放线程来处理不同的事情,它会增加我的多线程应用程序的吞吐量吗?不确定同步调用“阻塞”的程度有多深。我知道有时可能需要 300 多毫秒才能从 azure 表中获得响应。
对于上下文,我正在为 Photon Socket Server 实现编码 - 它使用线程池和光纤。我正在实现这样的接口(没有异步事件)
protected override void OnOperationRequest(OperationRequest o, SendParameters p)
我可以这样做,但是像这样在 Photon 中使用异步调用:requestFiber.Enqueue( () => DoStuff; );
/* synchronous version of code fragment */
entity = tableRetryPolicy.ExecuteAction( () => {
var result = table.Execute (retrieveOp);
if( result.HttpStatusCode == 404 )
return null;
App.CheckHttpStatusCode ("Get (Table) Retrieve", result, 200);
return (T) result.Result;
});
/* async version of code fragment */
var task = tableRetryPolicy.ExecuteAsync( () =>
table.ExecuteAsync (retrieveOp).ContinueWith( t =>
{
if (t.Exception != null)
{
// non-transient exception occurred or retry limit reached
App.log.Error("Get (Table) failed all retries: entityId: " + entityId);
}
else
{
if( t.Result.HttpStatusCode == 404 )
return;
App.CheckHttpStatusCode ("Get (Table) Retrieve", t.Result, 200);
entity = (T) t.Result.Result;
}
}
));
task.Wait();
// ^ dont have anything else we can do with this execution path, except wait!
【问题讨论】:
标签: .net multithreading azure asynchronous azure-table-storage