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

相关文章

Spring常见错误之Web嵌套对象校验失效解决办法

《Spring常见错误之Web嵌套对象校验失效解决办法》:本文主要介绍Spring常见错误之Web嵌套对象校验失效解决的相关资料,通过在Phone对象上添加@Valid注解,问题得以解决,需要的朋... 目录问题复现案例解析问题修正总结  问题复现当开发一个学籍管理系统时,我们会提供了一个 API 接口去

解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题

《解决mybatis-plus-boot-starter与mybatis-spring-boot-starter的错误问题》本文主要讲述了在使用MyBatis和MyBatis-Plus时遇到的绑定异常... 目录myBATis-plus-boot-starpythonter与mybatis-spring-b

【经验交流】修复系统事件查看器启动不能时出现的4201错误

方法1,取得『%SystemRoot%\LogFiles』文件夹和『%SystemRoot%\System32\wbem』文件夹的权限(包括这两个文件夹的所有子文件夹的权限),简单点说,就是使你当前的帐户拥有这两个文件夹以及它们的子文件夹的绝对控制权限。这是最简单的方法,不少老外说,这样一弄,倒是解决了问题。不过对我的系统,没用; 方法2,以不带网络的安全模式启动,运行命令行,输入“ne

用einsum实现MultiHeadAttention前向传播

einsum教程网站Einstein Summation in Numpy | Olexa Bilaniuk's IFT6266H16 Course Blog 编写训练模型 import tensorflow as tfclass Model(tf.keras.Model):def __init__(self, num_heads, model_dim):super().__init__

SQL2005 性能监视器计数器错误解决方法

【系统环境】 windows 2003 +sql2005 【问题状况】 用户在不正当删除SQL2005后会造成SQL2005 性能监视器计数器错误,如下图 【解决办法】 1、在 “开始” --> “运行”中输入 regedit,开启注册表编辑器,定位到 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVer

ssm 之事务管理出现错误

JDBC Connection will not be managed by Spring 项目采用的是分布式架构,分别有controller,service,solr三个服务器,之间通过dubbo进行调用,经过测试发现事务配置完以后不能通过spring进行管理,其中两条insert和一条update语句都执行完毕,异常并没有使得事务进行回滚,通过调取debug日志发现“JDBC Conn

Unstructured cannot write mode RGBA as JPEG 错误解决

Unstructured cannot write mode RGBA as JPEG 错误解决 0. 错误详细1. 解决方法 0. 错误详细 Image Extraction Error: Skipping the failed imageTraceback (most recent call last):File "/root/miniconda3/envs/learn-y

收藏:解决 pip install 出现 error: subprocess-exited-with-error 错误的方法

在使用 pip 安装 Python 包时,有时候会遇到 error: subprocess-exited-with-error 错误。这种错误通常是由于 setuptools 版本问题引起的。本文将介绍如何解决这一问题 当你使用 pip install 安装某个 Python 包时,如果 setuptools 版本过高或过低,可能会导致安装过程出错,并出现类似以下错误信息:error: subpr

插件:清理maven错误缓存.bat

插件:https://pan.baidu.com/s/1nHIxHoo1C4MvFlW7QbZe5Q?pwd=7zenhttps://pan.baidu.com/s/1nHIxHoo1C4MvFlW7QbZe5Q?pwd=7zen没错误缓存时: 有错误缓存时:

在幼儿园管理系统中,会议管理申请会议模块:添加会议记录(提交表单)的时候报:404错误!

在幼儿园管理系统(spring MVC)中,会议管理>申请会议模块:添加会议记录的时候报:404错误!不知道为啥找不到,一开始感觉一头雾水,怎么会出现404页面找不到错误那,又检查action,controller等这也没错啊!怎么出现404错误那。经过询问和查找,终于找到原因了。 原因是:添加的有时间字段。 代码: @InitBinder public void in