【发布时间】: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