diff options
-rw-r--r-- | heat/openstack/common/__init__.py | 17 | ||||
-rw-r--r-- | heat/openstack/common/threadgroup.py | 28 |
2 files changed, 40 insertions, 5 deletions
diff --git a/heat/openstack/common/__init__.py b/heat/openstack/common/__init__.py index e69de29bb..d1223eaf7 100644 --- a/heat/openstack/common/__init__.py +++ b/heat/openstack/common/__init__.py @@ -0,0 +1,17 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import six + + +six.add_move(six.MovedModule('mox', 'mox', 'mox3.mox')) diff --git a/heat/openstack/common/threadgroup.py b/heat/openstack/common/threadgroup.py index 176360a3b..a77414201 100644 --- a/heat/openstack/common/threadgroup.py +++ b/heat/openstack/common/threadgroup.py @@ -11,10 +11,10 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +import threading import eventlet from eventlet import greenpool -from eventlet import greenthread from heat.openstack.common import log as logging from heat.openstack.common import loopingcall @@ -51,7 +51,7 @@ class Thread(object): class ThreadGroup(object): - """The point of the ThreadGroup classis to: + """The point of the ThreadGroup class is to: * keep track of timers and greenthreads (making it easier to stop them when need be). @@ -85,8 +85,8 @@ class ThreadGroup(object): def thread_done(self, thread): self.threads.remove(thread) - def stop(self): - current = greenthread.getcurrent() + def _stop_threads(self): + current = threading.current_thread() # Iterate over a copy of self.threads so thread_done doesn't # modify the list while we're iterating @@ -99,6 +99,7 @@ class ThreadGroup(object): except Exception as ex: LOG.exception(ex) + def stop_timers(self): for x in self.timers: try: x.stop() @@ -106,6 +107,23 @@ class ThreadGroup(object): LOG.exception(ex) self.timers = [] + def stop(self, graceful=False): + """stop function has the option of graceful=True/False. + + * In case of graceful=True, wait for all threads to be finished. + Never kill threads. + * In case of graceful=False, kill threads immediately. + """ + self.stop_timers() + if graceful: + # In case of graceful=True, wait for all threads to be + # finished, never kill threads + self.wait() + else: + # In case of graceful=False(Default), kill threads + # immediately + self._stop_threads() + def wait(self): for x in self.timers: try: @@ -114,7 +132,7 @@ class ThreadGroup(object): pass except Exception as ex: LOG.exception(ex) - current = greenthread.getcurrent() + current = threading.current_thread() # Iterate over a copy of self.threads so thread_done doesn't # modify the list while we're iterating |