0

我正在编写一些 Python 代码来并行对大量 git 存储库执行操作。为此,我尝试结合concurrent.futuresGitPython,在单独的未来任务中克隆每个存储库。这是使用 OS X 10.10 上的内置 Python 2.7.6 以及通过 pip 安装的 GitPython 0.3.5 和 futures 2.2.0(向后移植到 2.7 的版本)。

我正在使用的代码的一个简单示例如下:

import time
from concurrent import futures
import shutil
import os
from git import Repo


def wait_then_return(i):
    print('called: %s', i)
    time.sleep(2)
    return i


def clone_then_return(i):
    print('called: %s', i)
    path = os.path.join('/tmp', str(i))
    os.mkdir(path)
    # clone some arbitrary repo
    Repo.clone_from('https://github.com/ros/rosdistro', path)
    shutil.rmtree(path)
    return i



if __name__ == "__main__":

    tasks = 20
    workers = 4

    with futures.ThreadPoolExecutor(max_workers=workers) as executor:

        # this works as expected... delaying work until a thread is available
        # fs = [executor.submit(wait_then_return, i) for i in range(0, tasks)]
        # this doesn't... all 20 come in quick succession
        fs = [executor.submit(clone_then_return, i) for i in range(0, tasks)]

        for future in futures.as_completed(fs):
            print('result: %s', future.result())

当我将wait_then_return函数提交给 executor 时,我得到了预期的行为:首先以四个为一组进行打印,然后大致沿着这些线完成,直到所有期货都完成。如果我切换它,clone_then_return那么执行程序似乎忽略了 max_workers参数并并行运行所有 20 个期货。

这可能是什么原因?

4

1 回答 1

1

实际上,我使用的 git 调用存在一些身份验证问题,导致未来快速完成。在并发的世界里一切都还算正常。

于 2015-01-23T01:36:59.590 回答