- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
由于 MicroSoft 所述的好处,我正在使用 Parallel.ForEach
实现大量 SQL SELECT 语句(其中有数千条) (顺便说一句:我现在找不到它,但我很确定我在某处读到 Parallel.ForEach 在执行完所有迭代之前不会返回控制权。这是真的吗?)
我从 Parallel.Foreach SQL querying sometimes results in Connection 开始.我不想使用已接受的答案,因为它放弃了 Parallel.ForEach
而只使用 Task.Run
(或 Task Factory.StartNew
对于 .Net 4.0)。同时,O.P. 使用“锁定”将更新同步到列表 DataTable
据我所知,这可能会降低 Parallel.ForEach
的效率.
因此,使用文章 How to: Write a Parallel.ForEach Loop with Thread-Local Variables ,我在下面写了稻草人代码。目标是积累 threadLocalDataTable
(每个选择语句一个)到 dataTableList
完成所有查询后返回。它有效,但我想知道 localFinally
方法确实是线程安全的(参见带有注释 //<-- the localFinally method in question
的代码行。注意:SqlOperation 类实现连接字符串、输出数据表名称和选择查询字符串
public static IList<DataTable> GetAllData(IEnumerable<SqlOperation> sqlList)
{
IList<DataTable> dataTableList = new List<DataTable>();
dataTableList.Clear();
try
{
Parallel.ForEach<SqlOperation, DataTable>(sqlList, () => null, (s, loop, threadLocalDataTable) =>
{
DataTable dataTable = null;
using (SqlCommand sqlCommand = new SqlCommand())
{
using (SqlConnection sqlConnection = new SqlConnection(s.ConnectionString))
{
sqlConnection.Open();
sqlCommand.CommandType = CommandType.Text;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = s.SelectQuery;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
dataTable = new DataTable
{
TableName = s.DataTableName
};
dataTable.Clear();
dataTable.Rows.Clear();
dataTable.Columns.Clear();
sqlDataAdapter.Fill(dataTable);
sqlDataAdapter.Dispose();
sqlConnection.Close();
}
}
return dataTable;
}, (threadLocalDataTable) => dataTableList.Add(threadLocalDataTable) //<-- the localFinally method in question
);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString(), "GetAllData error");
}
return dataTableList;
}
最佳答案
您提供的链接文档明确指出(关于 localFinally):
This delegate can be invoked concurrently by multiple tasks
所以不,它不是线程安全的。但这不是唯一的问题 - 您的整个实现都不正确。
传递给每个循环迭代的“线程本地”变量是累加器。它累积在同一线程上运行的多次迭代所获得的结果。您的实现完全忽略了该变量并始终返回一个数据表。这意味着如果您的表多于并行循环线程数(默认为处理器的核心数)- 您实际上正在丢失结果,因为您忽略了累加器。
正确的方法是使用 List<DataTable>
作为蓄能器,而不是一个。例如:
Parallel.ForEach<SqlOperation, List<DataTable>>(
sqlList,
() => new List<DataTable>(), // initialize accumulator
(s, loop, threadLocalDataTables) =>
{
DataTable result = ...;
// add, that's thread safe
threadLocalDataTables.Add(result);
// return accumulator to next iteration
return threadLocalDataTables;
}, (threadLocalDataTables) =>
{
//<-- the localFinally method in question
lock (dataTableList) // lock and merge results
dataTableList.AddRange(threadLocalDataTables);
});
就是说 - 使用 Parallel.ForEach
加速 IO 绑定(bind)的工作(比如进行 SQL 查询)不是一个好主意,使用 async\await 会做得更好。但这超出了这个问题的范围。
关于c# - 这个 C# Parallel.ForEach "localFinally"方法线程安全吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49327203/
在 Oracle 中,PARALLEL 被广泛使用。提示 PARALLEL、PARALLEL(8) 和 PARALLEL(a,8) 有什么区别。如何选择最佳的查询提示? SELECT /*+ PARA
好的,我希望以前没有问过这个问题,因为在搜索中很难找到。 我查看了 F95 手册,但仍然觉得这很模糊: For the simple case of: DO i=0,99 END DO 我正
我有一个 C-shell 脚本,其中有一个名为 $hosts_string 的变量,格式为: host1,host2,...,hostN 我还有一个名为 $chrs_string 的变量,其形式为:
是否可以从由gnu parallel产生的脚本的多次运行中调用gnu parallel? 我有一个python脚本,可以运行100个顺序顺序迭代,并且在每次迭代中的某处,并行计算4个值(使用gnu p
我想在几个输入上运行几个长时间运行的进程。例如。: solver_a problem_1 solver_b problem_1 ... solver_b problem_18 solver_c pro
TParallel.&For 和 TParallel.For 之间有区别吗? 两者都可以在 Delphi 10 Seattle 中编译。那么我应该坚持哪一个呢? 最佳答案 TParallel.&For
我第一次使用 julia 进行并行计算.我有点头疼。所以假设我开始 julia如下:julia -p 4 .然后我为所有处理器声明 a 函数,然后将它与 pmap 一起使用还有@parallel fo
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 10年前关闭。 Improve this
我有一堆相互排斥的方法,因此可以并行运行。有这样做的好方法吗?到目前为止,我有以下两种实现方式,但我不确定是否应该选择其中一种。 使用 Parallel.For : Parallel.For(0, 2
我对并行运行脚本很感兴趣,并且我已经开始查看 GNU 并行工具,但是我遇到了一些麻烦。我的脚本 doSomething 有 3 个参数,我想在参数的不同值上并行运行脚本。我该怎么做? 我试过:para
我需要在多核(和多线程)机器上运行多个作业。我正在使用 GNU Parallel utility跨核心分配作业以加速任务。要执行的命令在名为“命令”的文件中可用。我使用以下命令运行 GNU Paral
我正在尝试使用如下两个输入运行 Python 脚本。我得到了大约 300 个这两个输入,所以我想知道是否有人可以建议如何并行运行它们。 单次运行看起来像: python stable.py KOG_1
每天我都必须更新一堆存储库,并在其中一些中执行另一个命令(来自 CARTON,Perl 模块依赖管理器)。我总是使用循环来执行此操作,但我想与 并行执行GNU 并行 如果可能,但我不太了解它的tuto
正如标题所说:@parallel 之间究竟有什么区别?和 pmap ?我的意思不是明显的一个是循环的宏,另一个适用于函数,我的意思是它们的实现究竟有什么不同,我应该如何使用这些知识在它们之间进行选择?
我有一些矩阵乘法运算。我想通过多个处理器并行执行这些操作。这可以使用 MPI(消息传递接口(interface))在高性能计算集群上完成。 同样,我可以使用多个辅助角色在云中进行一些并行化吗?有什么办
joblib模块提供了一个简单的帮助程序类,以使用多处理并行编写循环的循环。 这段代码使用列表推导来完成这项工作: import time from math import sqrt from job
我的问题是这样的one .但我想做一些不同的事情... 例如,在我的并行区域内,我想在 4 个线程上运行我的代码。当每个线程进入 for 循环时,我想在 8 个线程上运行我的代码。像 #pramga
我正在尝试使用 ipython 并行库中的并行计算。但是我对此知之甚少,而且我发现很难从对并行计算一无所知的人那里阅读该文档。 有趣的是,我发现的所有教程都只是重复使用文档中的示例,并使用相同的解释,
我的项目结构看起来像 Root + subproj1 + subproj2 在每个子项目中定义了自己的任务 run(){}。 我想要做的是从 Root 项目的运行任务并行运行 :subpro
我有一个 Foo ID 的列表。我需要为每个 ID 调用一个存储过程。 例如 Guid[] siteIds = ...; // typically contains 100 to 300 elemen
我是一名优秀的程序员,十分优秀!