Concurrency in CSharp Cookbook中文翻译:5.2传播错误

2024-01-05 04:04

本文主要是介绍Concurrency in CSharp Cookbook中文翻译:5.2传播错误,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Problem 问题

You need a way to respond to errors that can happen in your dataflow mesh.
您需要一种方法来响应数据流网格中可能发生的错误。

Solution 解决方法

If a delegate passed to a dataflow block throws an exception, then that block will enter a faulted state. When a block is in a faulted state, it will drop all of its data (and stop accepting new data). The block in the following code will never produce any output data; the first value raises an exception, and the second value is just dropped:
如果传递给数据流块的委托抛出异常,则该块将进入故障状态。当一个块处于故障状态时,它将丢弃所有数据(并停止接受新数据)。下面代码中的块永远不会产生任何输出数据;第一个值引发异常,第二个值被丢弃:
var block = new TransformBlock(item =>{ if (item == 1)throw new InvalidOperationException("Blech."); return item * 2; }); 
block.Post(1); 
block.Post(2);
To catch exceptions from a dataflow block, you should await its Completion property. The Completion property returns a Task that will complete when the block is completed, and if the block faults, the Completion task is also faulted:
要从数据流块捕获异常,应该等待它的Completion属性。Completion属性返回一个任务,该任务将在块完成时完成,如果块出现故障,则Completion任务也会出现故障:
try 
{ var block = new TransformBlock(item =>{ if (item == 1)throw new InvalidOperationException("Blech."); return item * 2; }); block.Post(1); await block.Completion; 
} 
catch (InvalidOperationException) 
{ // The exception is caught here. 
}
When you propagate completion using the PropagateCompletion link option,errors are also propagated. However, the exception is passed to the next block wrapped in an AggregateException. The following example catches the exception from the end of a pipeline, so it would catch AggregateException if an exception was propagated from earlier blocks:
当您使用PropagateCompletion链接选项传播完成时,也会传播错误。然而,异常被传递到封装在AggregateException中的下一个块。下面的例子从管道的末端捕获异常,所以如果一个异常是从早期块传播的,它将捕获AggregateException:
try
{var multiplyBlock = new TransformBlock<int, int>(item =>{if (item == 1)throw new InvalidOperationException("Blech.");return item * 2;});var subtractBlock = new TransformBlock<int, int>(item => item - 2);multiplyBlock.LinkTo(subtractBlock,new DataflowLinkOptions { PropagateCompletion = true });multiplyBlock.Post(1);await subtractBlock.Completion;
}
catch (AggregateException)
{// The exception is caught here.
}
When you propagate completion using the PropagateCompletion link option,errors are also propagated. However, the exception is passed to the next block wrapped in an AggregateException. Each block wraps incoming errors in an AggregateException, even if the incoming error is already an AggregateException. If an error occurs early in a pipeline and travels down several links before it’s observed, the original error will be wrapped in multiple layers of AggregateException. The AggregateException.Flatten method simplifies error handling in this scenario.The following example catches the exception from the end of a pipeline, so it would catch AggregateException if an exception was propagated from earlier blocks:
当您使用PropagateCompletion链接选项传播完成时,也会传播错误。然而,异常被传递到封装在AggregateException中的下一个块。每个块将传入的错误包装在AggregateException中,即使传入的错误已经是AggregateException。如果错误发生在管道的早期,并且在观察到它之前沿着几个链接传播,那么原始错误将被包装在多层AggregateException中。AggregateException。Flatten方法简化了这种情况下的错误处理。下面的例子从管道的末端捕获异常,所以如果一个异常是从早期块传播的,它将捕获AggregateException:

Discussion 讨论

When you build your mesh (or pipeline), consider how errors should be handled. In simpler situations, it can be best to just propagate the errors and catch them once at the end. In more complex meshes, you may need to observe each block when the dataflow has completed.
当您构建网格(或管道)时,请考虑如何处理错误。在更简单的情况下,最好只传播错误并在最后捕获一次。在更复杂的网格中,您可能需要在数据流完成时观察每个块。
Alternatively, if you want your blocks to remain viable in the face of exceptions, you can choose to treat exceptions as another kind of data and let them flow through your mesh along with your correctly processed data items. Using that pattern, you can keep your dataflow mesh operational, since the blocks themselves don’t fault and continue processing the next data item. See Recipe 14.6 for more details.
或者,如果您希望您的块在面对异常时仍然可行,您可以选择将异常视为另一种数据,并让它们与正确处理的数据项一起流经网格。使用该模式,您可以保持数据流网格的可操作性,因为块本身不会出错并继续处理下一个数据项。参见Recipe 14.6了解更多细节。

这篇关于Concurrency in CSharp Cookbook中文翻译:5.2传播错误的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/571596

相关文章

深度解析Java @Serial 注解及常见错误案例

《深度解析Java@Serial注解及常见错误案例》Java14引入@Serial注解,用于编译时校验序列化成员,替代传统方式解决运行时错误,适用于Serializable类的方法/字段,需注意签... 目录Java @Serial 注解深度解析1. 注解本质2. 核心作用(1) 主要用途(2) 适用位置3

Debian 13升级后网络转发等功能异常怎么办? 并非错误而是管理机制变更

《Debian13升级后网络转发等功能异常怎么办?并非错误而是管理机制变更》很多朋友反馈,更新到Debian13后网络转发等功能异常,这并非BUG而是Debian13Trixie调整... 日前 Debian 13 Trixie 发布后已经有众多网友升级到新版本,只不过升级后发现某些功能存在异常,例如网络转

SpringBoot3匹配Mybatis3的错误与解决方案

《SpringBoot3匹配Mybatis3的错误与解决方案》文章指出SpringBoot3与MyBatis3兼容性问题,因未更新MyBatis-Plus依赖至SpringBoot3专用坐标,导致类冲... 目录SpringBoot3匹配MyBATis3的错误与解决mybatis在SpringBoot3如果

nginx配置错误日志的实现步骤

《nginx配置错误日志的实现步骤》配置nginx代理过程中,如果出现错误,需要看日志,可以把nginx日志配置出来,以便快速定位日志问题,下面就来介绍一下nginx配置错误日志的实现步骤,感兴趣的可... 目录前言nginx配置错误日志总结前言在配置nginx代理过程中,如果出现错误,需要看日志,可以把

Python错误AttributeError: 'NoneType' object has no attribute问题的彻底解决方法

《Python错误AttributeError:NoneTypeobjecthasnoattribute问题的彻底解决方法》在Python项目开发和调试过程中,经常会碰到这样一个异常信息... 目录问题背景与概述错误解读:AttributeError: 'NoneType' object has no at

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

Spring事务传播机制最佳实践

《Spring事务传播机制最佳实践》Spring的事务传播机制为我们提供了优雅的解决方案,本文将带您深入理解这一机制,掌握不同场景下的最佳实践,感兴趣的朋友一起看看吧... 目录1. 什么是事务传播行为2. Spring支持的七种事务传播行为2.1 REQUIRED(默认)2.2 SUPPORTS2

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

如何解决Druid线程池Cause:java.sql.SQLRecoverableException:IO错误:Socket read timed out的问题

《如何解决Druid线程池Cause:java.sql.SQLRecoverableException:IO错误:Socketreadtimedout的问题》:本文主要介绍解决Druid线程... 目录异常信息触发场景找到版本发布更新的说明从版本更新信息可以看到该默认逻辑已经去除总结异常信息触发场景复

Python struct.unpack() 用法及常见错误详解

《Pythonstruct.unpack()用法及常见错误详解》struct.unpack()是Python中用于将二进制数据(字节序列)解析为Python数据类型的函数,通常与struct.pa... 目录一、函数语法二、格式字符串详解三、使用示例示例 1:解析整数和浮点数示例 2:解析字符串示例 3:解