gpt4 book ai didi

c# - 并发受限的 TaskScheduler 会阻塞自身

转载 作者:太空狗 更新时间:2023-10-29 22:30:48 24 4
gpt4 key购买 nike

我正在尝试使用 the following custom task scheduler限制并发网络请求:

我是这样使用它的:

const int CONCURRENCY_LEVEL = 2;

static void Main()
{
TaskFactory factory = new TaskFactory(new LimitedConcurrencyLevelTaskScheduler(CONCURRENCY_LEVEL));

Task.WaitAll(new[] { factory.StartNew(() => SomeAction("first")), factory.StartNew(() => SomeAction("second")) });

Console.WriteLine("All tasks ended");
Console.Read();
}

static void SomeAction(string task)
{
Console.WriteLine(task + " task started");
Console.WriteLine(AnotherTask(task).Result);
Console.WriteLine(task + " task ended");
}

static async Task<string> AnotherTask(string task)
{
return await Task.Run(() =>
{
Console.WriteLine(task + " inner task started");
// imitate web requests
Thread.Sleep(200);
Console.WriteLine(task + " inner task ended");
return "ok";
});
}

问题是内部任务正在使用我的 LimitedConcurrencyLevelTask​​Scheduler 而不是默认的 TaskScheduler。这会导致 self 阻塞,输出如下:

second task started
first task started
first inner task started
second inner task started
second inner task ended
first inner task ended

当我将 CONCURRENCY_LEVEL 更改为 3 或将 factory 更改为 Task.Factory 时,显然一切都是工作正常,输出如下:

first task started
second task started
first inner task started
second inner task started
second inner task ended
first inner task ended
ok
second task ended
ok
first task ended
All tasks ended

无法更改SomeAction 中的代码。我还可以做些什么? Microsoft 示例中是否存在错误?

最佳答案

从自定义任务计划程序启动任务时指定TaskCreationOptions.HideScheduler:

Task.WaitAll(new[] 
{
factory.StartNew(() => SomeAction("first"), TaskCreationOptions.HideScheduler),
factory.StartNew(() => SomeAction("second"), TaskCreationOptions.HideScheduler)
});

来自 MSDN :

HideScheduler Prevents the ambient scheduler from being seen as the current scheduler in the created task. This means that operations like StartNew or ContinueWith that are performed in the created task will see Default as the current scheduler.

关于c# - 并发受限的 TaskScheduler 会阻塞自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28374605/

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