如何将'for'循环与外部变量并行化?

时间:2021-11-15 08:32:28

I am having difficulties parallelizing that part of code where new_text is of type unicode:

我难以并行化new_text类型为unicode的代码部分:

for old, new in self.replacements:
    line = pywikibot.replaceExcept(
        line, old, new, self.excsInside, self.site)
if new_text != entry.text:
    yield pywikibot.Page(self.site, entry.title)

The task looks to be easy with joblib or a process-pool, but there is new_text which is used outside of the loop. I have no idea of the equivalent of#pragama omp orderedor#pragma omp atomic, since there is no OpenMP wrapper for Python...

使用joblib或进程池,任务看起来很容易,但是在循环之外使用new_text。我不知道#pragama omp orderedor#pragma omp atomic的等价物,因为没有Python的OpenMP包装器......

How do I determine what the value of new_next is going to be in the if statement if it's run in parallel?

如果并行运行,如何确定new_next在if语句中的值?

1 个解决方案

#1


0  

As it is inherently sequential, you can parallelize per line:

由于它本质上是顺序的,因此您可以按行并行化:

for line in new_text
    for old, new in self.replacements:
        line = pywikibot.replaceExcept(
            line, old, new, self.excsInside, self.site)

Ánd you parallelize the outer for loop (map with each replacement and then reduce by concatenation).

您并行外部for循环(映射每个替换,然后通过连接减少)。

#1


0  

As it is inherently sequential, you can parallelize per line:

由于它本质上是顺序的,因此您可以按行并行化:

for line in new_text
    for old, new in self.replacements:
        line = pywikibot.replaceExcept(
            line, old, new, self.excsInside, self.site)

Ánd you parallelize the outer for loop (map with each replacement and then reduce by concatenation).

您并行外部for循环(映射每个替换,然后通过连接减少)。