【发布时间】:2018-08-03 01:23:22
【问题描述】:
我在 localHost 集群模式下运行 Orleans,目前有 1 个grain 和一个客户端。
// client code
for (int i = 0; i <num_scan; ++i)
{
Console.WriteLine("client " + i);
// the below call should have returned when first await is hit in foo()
// but it doesn't work like that
grain.foo(i);
}
// grain code
async Task foo(int i)
{
Console.WriteLine("grain "+i);
await Task.Delay(2000);
}
输出如下:
client 0
client 1
client 2
client 3
client 4
client 5
client 6
grain 0
client 7
client 8
client 9
client 10
grain 8
grain 7
.
.
在normal C# 中,异步函数仅在遇到await 时才返回。在这种情况下,粮食产量应该是连续的。正如我们在上面看到的,谷物输出是乱序的。因此,任务在到达await 语句之前返回。 我的问题是奥尔良中的方法调用和普通 C# 有什么区别。
我看到this post问了一个类似的问题,回复表明这两种方法调用的情况是不同的,因为我们在奥尔良调用了一个接口。 我想知道,奥尔良的方法调用什么时候返回。
PS: 我用await grain.foo() 尝试了上面的代码,它按顺序打印谷物输出。但是这种方法的问题是,await 仅在整个 foo() 完成时才返回,而我希望它在遇到 await 语句时返回。
【问题讨论】:
-
您实际上想要完成什么?您是否希望颗粒并行运行但以确定的顺序返回?
-
From this document,看起来像谷子生成的任务在谷子上下文中执行。文档建议您可以使用
Task.Run(() => {...})来代替使用默认任务计划程序。
标签: c# asp.net async-await orleans