gpt4 book ai didi

azure - azure 辅助角色中的异步/等待导致角色回收

转载 作者:行者123 更新时间:2023-12-03 23:26:31 25 4
gpt4 key购买 nike

我正在我的 WorkerRole (RoleEntryPoint) 中使用任务、异步和等待。

我有一些无法解释的回收,现在我发现,如果等待调用中某些内容运行时间过长,则角色会回收。要重现它,只需在 Run 方法中执行 await Task.Delay(60000) 即可。

谁能帮我解释一下为什么?

最佳答案

Run 方法必须 被阻止。来自 the docs :

If you do override the Run method, your code should block indefinitely. If the Run method returns, the role is automatically recycled by raising the Stopping event and calling the OnStop method so that your shutdown sequences may be executed before the role is taken offline.

一个简单的解决方案就是这样做:

public override void Run()
{
RunAsync().Wait();
}

public async Task RunAsync()
{
while (true)
{
await Task.Delay(60000);
}
}

或者,您可以使用AsyncContext from my AsyncEx library :

public override void Run()
{
AsyncContext.Run(async () =>
{
while (true)
{
await Task.Delay(60000);
}
});
}

无论您选择哪个选项,运行都不应该异步。它有点像控制台应用程序的 Main ( see my blog for why async Main is not allowed )。

关于azure - azure 辅助角色中的异步/等待导致角色回收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15991287/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com