Python 线程:监视线程并在线程完成后执行附加代码
Python threading: monitoring threads and executing additional code after threads complete
我有以下代码:
class MyThread(Thread):
def __init__(self, command, template, env, build_flavor, logger):
Thread.__init__(self)
self.command = command
self.template = template
self.env = env
self.build_flavor = build_flavor
self.logger = logger
def run(self):
self.logger.info('Running (%s)...this may take several minutes. Please be patient' % self.build_flavor)
run_command(self.command, self.template, self.env)
self.logger.info('Complete (%s)' % self.build_flavor)
return
然后在另一个 class 中,当我创建实际线程时:
if self.build_type == 'default':
threads = []
for t in self.template:
modify_template(t)
build_flavor = self.getmatch(t)
thread = MyThread(packer, t, self.new_env, build_flavor, self.logger)
thread.setName(build_flavor)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
vmware_create()
openstack_create()
不幸的是,在线程被 .join() 之后,我在串行调用 vmware_create() 和 openstack_create()。我希望能够在它们各自的线程完成后执行它们中的每一个,这样我就不会在启动其中一个 *_create() 函数之前等待两个线程完成......然后等待第一个完成在执行第二个
之前
即现在 vmware_create() 只会在两个线程都完成后执行,一旦 vmware_create() 完成,openstack_create() 才会开始。我希望能够等待各自的线程完成,然后为首先完成的任何线程执行 _create() 函数,一直等待第二个线程完成,然后一旦完成,立即执行其 _create () 真正并行化的函数。
我还不知道该怎么做,需要一点帮助。
函数是对象。只需将它们交给线程即可:
class MyThread(Thread):
def __init__(self, command, template, env, build_flavor, logger, func=None):
Thread.__init__(self)
self.command = command
self.template = template
self.env = env
self.build_flavor = build_flavor
self.logger = logger
self.func = func
def run(self):
self.logger.info('Running (%s)...this may take several minutes. Please be patient' % self.build_flavor)
run_command(self.command, self.template, self.env)
self.logger.info('Complete (%s)' % self.build_flavor)
# call func if it is there
if self.func:
self.func()
return
现在,我为前两个线程提供要调用的函数:
if self.build_type == 'default':
threads = []
funcs = {0: vmware_create, 1: openstack_create}
for i, t in enumerate(self.template):
modify_template(t)
build_flavor = self.getmatch(t)
func = funcs.get(i, None)
thread = MyThread(packer, t, self.new_env, build_flavor,
self.logger, func=func)
thread.setName(build_flavor)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
当然,您可以将它们添加到任何其他线程。
我有以下代码:
class MyThread(Thread):
def __init__(self, command, template, env, build_flavor, logger):
Thread.__init__(self)
self.command = command
self.template = template
self.env = env
self.build_flavor = build_flavor
self.logger = logger
def run(self):
self.logger.info('Running (%s)...this may take several minutes. Please be patient' % self.build_flavor)
run_command(self.command, self.template, self.env)
self.logger.info('Complete (%s)' % self.build_flavor)
return
然后在另一个 class 中,当我创建实际线程时:
if self.build_type == 'default':
threads = []
for t in self.template:
modify_template(t)
build_flavor = self.getmatch(t)
thread = MyThread(packer, t, self.new_env, build_flavor, self.logger)
thread.setName(build_flavor)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
vmware_create()
openstack_create()
不幸的是,在线程被 .join() 之后,我在串行调用 vmware_create() 和 openstack_create()。我希望能够在它们各自的线程完成后执行它们中的每一个,这样我就不会在启动其中一个 *_create() 函数之前等待两个线程完成......然后等待第一个完成在执行第二个
之前即现在 vmware_create() 只会在两个线程都完成后执行,一旦 vmware_create() 完成,openstack_create() 才会开始。我希望能够等待各自的线程完成,然后为首先完成的任何线程执行 _create() 函数,一直等待第二个线程完成,然后一旦完成,立即执行其 _create () 真正并行化的函数。
我还不知道该怎么做,需要一点帮助。
函数是对象。只需将它们交给线程即可:
class MyThread(Thread):
def __init__(self, command, template, env, build_flavor, logger, func=None):
Thread.__init__(self)
self.command = command
self.template = template
self.env = env
self.build_flavor = build_flavor
self.logger = logger
self.func = func
def run(self):
self.logger.info('Running (%s)...this may take several minutes. Please be patient' % self.build_flavor)
run_command(self.command, self.template, self.env)
self.logger.info('Complete (%s)' % self.build_flavor)
# call func if it is there
if self.func:
self.func()
return
现在,我为前两个线程提供要调用的函数:
if self.build_type == 'default':
threads = []
funcs = {0: vmware_create, 1: openstack_create}
for i, t in enumerate(self.template):
modify_template(t)
build_flavor = self.getmatch(t)
func = funcs.get(i, None)
thread = MyThread(packer, t, self.new_env, build_flavor,
self.logger, func=func)
thread.setName(build_flavor)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
当然,您可以将它们添加到任何其他线程。