【问题标题】:Python - Running many different types of threads without exhausting resourcesPython - 在不耗尽资源的情况下运行许多不同类型的线程
【发布时间】:2017-05-31 17:57:45
【问题描述】:

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

我有一个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,这样我就不会耗尽系统的资源。

【问题讨论】:

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


    【解决方案1】:

    正如您在问题的标签中已经说过的那样:使用游泳池。只要您的*Fix()s 调用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())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-13
      • 2011-03-24
      • 2011-04-17
      • 1970-01-01
      • 1970-01-01
      • 2017-12-03
      • 2018-10-28
      • 1970-01-01
      相关资源
      最近更新 更多