Python - 运行许多不同类型的线程而不会耗尽资源

时间:2017-05-31 17:57:46

标签: threadpool python-3.5 python-multithreading python-multiprocessing

我正在尝试做一些干净的多线程,但也许我的方法是错误的。

我有一个BaseFix Thread类,有6种不同类型的子类,其中一个是CommandFix Thread,另一个是ParamFix Thread。

他们的run函数中包含不同的代码,但主要使用subprocess.run函数。

class CommandFix(BaseFix):

    def __init__(self, group=None, target=None, name=None,
             args=(), kwargs=None, verbose=None):

        super(CommandFix, self).__init__(group=group, target=target, 
                      name=name, kwargs=kwargs, verbose=None)


    def _execute_command(self):
        self.stdout_log.write("fixing {} : running command {}".format(
            self.rule_id,
            self.command))

        try:
            cp = subprocess.run(self.command,
                shell=True, check=True,
                stdout=self.stdout_log,
                stderr=self.stderr_log
                )
        except subprocess.CalledProcessError as e:
            self.stderr_log.write("error fixing {} : {}".format(
                self.rule_id,
                e))

            # TO-DO: Handle error with retry or similar
            # right now just return false
            return False

        return True

    def _verify_command(self):
        pass

    def run(self):

        # execute the command
        if self._execute_command():
            # now run the verification
            pass
        else:
            # TO-DO: error handling
            pass

我对大约200种不同类型的6修正案。我正在检查他们的修复类型,然后实例化所需的修复并调用run,如下所示:

def _fix(key, rule):
    expr = "./{}fixtext".format(NS)
    fixtext = rule.find(expr)

    expr = "./{}div".format(NS1)
    fixtext = fixtext.find(expr).getchildren()[0].getchildren()[0].text

    if fixtext.find('Run the following command') > 0:
        fix = CommandFix(rule, stdout_log, stderr_log, key)

    elif fixtext.find('Set the following') > 0:
        # fix = SomeOtherFix()
        pass

    elif fixtext.find('For 32 bit systems') > 0:
        # fix = SomeOtherKindOfFix()
        pass

    fix.run()

# there are only 200 but assume there are 2 million
for key, rule in rules.items():
    _fix(key, rule)

我想要做的是清理它,以便我一次只执行X number of fixes,这样我就不会耗尽系统的资源。

1 个答案:

答案 0 :(得分:1)

正如您在问题的标签中所说:使用游泳池。只要您的*Fix()调用subprocess.run(),它们就会产生新的进程以实现真正的并行性。因此,线程池就足够了:

from multiprocessing.dummy import Pool    # wrapper around the threading module
#from multiprocessing import Pool         # real processes instead of threads
[...]
pool = Pool(5)                            # number of fixes at once
pool.starmap(_fix, rules.items())