我有以下代码(https://github.com/avinash0161/OrleansExperiments/tree/c0155b4b0c8c1bfe60aea8624f2cc83a52853dc7):
// Client code
Console.WriteLine("Client making a call");
var hashGenerator = client.GetGrain<IGrainA>(0);
hashGenerator.Call_A_ToTemp();
await Task.Delay(1000);
hashGenerator.Call_B_ToTemp();
// GrainA code
public async Task Call_A_ToTemp()
{
Console.WriteLine("Making call A to a fellow grain");
IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);
grain.CallA().ContinueWith((t)=>
{
if(t.IsFaulted)
{
// Silo message timeout is 32s so t.IsFaulted is true
Console.WriteLine("Task Faulted");
Call_A_ToTemp();
}
});
}
public async Task Call_B_ToTemp()
{
Console.WriteLine("Making call B to a fellow grain");
IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);
await grain.CallB();
}
// GrainB code
public async Task CallA()
{
Console.WriteLine("Call A came to GrainB");
await Task.Delay(34000); // more than timeout for the caller
}
public Task CallB()
{
Console.WriteLine("Call B came to GrainB");
return Task.CompletedTask;
}
此代码的输出为:
Client making a call
Making call A to a fellow grain
Call A came to GrainB
Making call B to a fellow grain
Task Faulted <---------------- This comes after Call_B_ToTemp executes
Making call A to a fellow grain
如我们所见,Call_B_ToTemp在Call_A_ToTemp完全执行之前执行(ContinueWith 部分Call_A_ToTemp稍后执行)。这是意料之中的,是否违反了颗粒的单线程性质?
当我将 Call_A_ToTemp() 中的代码替换为:
public async Task Call_A_ToTemp()
{
Console.WriteLine("Making call A to a fellow grain");
IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);
bool isSuccess = false;
while (! isSuccess)
{
try
{
await grain.CallA();
isSuccess = true;
} catch(TimeoutException){
Console.WriteLine("task faulted");
}
}
}
代码现在保留了单线程性质,并且在执行Call_A_ToTemp() 的所有部分之前不会调用Call_B_ToTemp ContinueWith
。控制台输出如下所示:
Client making a call
Making call A to a fellow grain
Call A came to GrainB
Task Faulted
Making call A to a fellow grain
谁能解释一下?当有ContinueWith
时是否违反了单线程性质?
单线程性质没有被违反。项目中的编译警告使问题的根源一目了然。特别是: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
该方法async Task Call_A_ToTemp()
永远不会等待对谷物 B 的调用。相反,它会在发出调用后立即返回。由于 Call_A_ToTemp()
返回的Task
会立即完成,因此允许对粒度执行另一个调用。一旦grain.CallA()
完成,延续(ContinueWith(...)
)将尽快在谷物的TaskScheduler
上执行(例如,当谷物等待另一个调用或闲置时)。
相反,如果调用已等待,或者如果从方法中删除async
并且代码更改为返回grain.CallA().ContinueWith(...)
调用,则将观察到预期的行为。即,将代码更改为此代码将为您提供预期的结果:
// removed 'async' here, since we're not awaiting anything.
// using 'async' is preferred, but this is to demonstrate a point about
// using ContinueWith and un-awaited calls
public Task Call_A_ToTemp()
{
Console.WriteLine("Making call A to a fellow grain");
IGrainB grain = this.GrainFactory.GetGrain<IGrainB>(1);
// Note the 'return' here
return grain.CallA().ContinueWith((t)=>
{
if(t.IsFaulted)
{
// Silo message timeout is 32s so t.IsFaulted is true
Console.WriteLine("Task Faulted");
Call_A_ToTemp();
}
});
}