简体   繁体   English

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

[英]How I can parallelize a 'for' loop with an external variable?

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. 使用joblibprocess-pool看起来任务很容易,但是在循环外部使用了new_text I have no idea of the equivalent of #pragama omp ordered or #pragma omp atomic , since there is no OpenMP wrapper for Python ... 我不知道#pragama omp ordered#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语句中?

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循环(映射每个替换,然后通过串联减少)。

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

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