简体   繁体   English

C#Parallel.Foreach等效于Python

[英]C# Parallel.Foreach equivalent in Python

I have 96 txt files that have to be processed. 我有96个txt文件需要处理。 Right now I am using a for loop and doing them one at a time, this process is very slow. 现在我正在使用for循环并一次执行一个,这个过程非常慢。 The resulting 96 files, do not need to be merged. 生成的96个文件,不需要合并。 Is there a way to make them run in parallel, ala Parallel.foreach in C#? 有没有办法使它们并行运行,ala Parallel.foreach在C#中? Current code: 当前代码:

for src_name in glob.glob(source_dir+'/*.txt'):
   outfile = open (...)
   with open(...) as infile:
      for line in infile:
         --PROCESS--
   for --condition--:
      outfile.write(...)
   infile.close()
   outfile.close()

Want this process to run in parallel for all files in source_dir. 希望此进程并行运行source_dir中的所有文件。

Assuming that the limiting factor is indeed the processing and not the I/O, you can use joblib to easily run your loop on multiple CPUs. 假设限制因素确实是处理而不是I / O,您可以使用joblib在多个CPU上轻松运行循环。

A simple example from their documentation : 他们的文档中的一个简单示例

>>> from math import sqrt
>>> from joblib import Parallel, delayed
>>> Parallel(n_jobs=2)(delayed(sqrt)(i ** 2) for i in range(10))
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM