何时应将 AllowSynchronousContinuations 选项与 Channels 一起使用?

When AllowSynchronousContinuations option should be used with Channels?

我正在 Asp Net Core 应用程序中实现简单的后台作业队列。我创建了 BackgroundJobQueue,它在后台使用 BoundedChannel<T> 来排队将通过 HostedService 处理的项目。阅读文档我偶然发现了频道的 ChannelOptions.AllowSynchronousContinuations 设置。

根据msdn的描述说:

Setting this option to true can provide measurable throughput improvements by avoiding scheduling additional work items. However, it may come at the cost of reduced parallelism, as for example a producer may then be the one to execute work associated with a consumer, and if not done thoughtfully, this can lead to unexpected interactions. The default is false.

我不太明白在我的情况下将此选项设置为 true 是否是一个好的选择。当此选项为 usefull/useless/harmfull 时,有人可以解释 + 提供示例吗?

编辑

我得到的解释:

According to the official statement, when producer depends on consumer, it waits for consumer's job to be done then starts its work, if you Enable the option, producer experiences more idle time. However, if you Disabled the option, because of parallelism, producer experiences lower idle time.

是不是开启选项不好?由于 api 请求将需要更长的时间来处理,因为生产者将保持空闲更长时间。澄清我的意思。假设我想在我的控制器中排队后台作业

public async Task<IActionResult> Action()
{
    // some code
   await _backgroundJobQueue(() => ....);
   return Ok();
}

如果启用该选项,那么生产者会经历更多空闲时间,因此需要更长时间才能执行?

您有后台队列。因此,您正在使用排队作业来同步操作。最好 Enable 它,因为您不需要并行性,并且文档声称“它提供了可衡量的吞吐量改进

as for example a producer may then be the one to execute work associated with a consumer

按照官方的说法,当producer依赖consumer时,它会等待consumer的工作完成后才开始工作,如果你Enable选项,producer会有更多的空闲时间。但是,如果您 Disabled 该选项,由于并行性,生产者的空闲时间会减少。

这是我的理解和例证