[PATCH 1/3] Allow having unique thread names with given prefix

Vratislav Podzimek vpodzime at redhat.com
Wed Nov 13 11:52:40 UTC 2013


We need to have unique thread names, but sometimes we need to run the same
action multiple times. Instead of inventing the whell over and over again in
multiple places, we may build-in the functionality in the AnacondaThread class
itself.

Also return thread name when it is added to the ThreadManager.

Signed-off-by: Vratislav Podzimek <vpodzime at redhat.com>
---
 pyanaconda/threads.py                      | 19 +++++++++++++++++++
 pyanaconda/ui/gui/spokes/datetime_spoke.py |  7 +------
 pyanaconda/ui/tui/simpleline/base.py       |  8 +-------
 3 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/pyanaconda/threads.py b/pyanaconda/threads.py
index 4bc01b6..77cc306 100644
--- a/pyanaconda/threads.py
+++ b/pyanaconda/threads.py
@@ -24,6 +24,8 @@ log = logging.getLogger("anaconda")
 
 import threading
 
+_WORKER_THREAD_PREFIX = "AnaWorkerThread"
+
 class ThreadManager(object):
     """A singleton class for managing threads and processes.
 
@@ -55,6 +57,8 @@ class ThreadManager(object):
         self._errors[obj.name] = None
         obj.start()
 
+        return obj.name
+
     def remove(self, name):
         """Removes a thread from the list of known objects.  This should only
            be called when a thread exits, or there will be no way to get a
@@ -155,7 +159,22 @@ class AnacondaThread(threading.Thread):
        (3) All created threads are made daemonic, which means anaconda will quit
            when the main process is killed.
     """
+
+    # class-wide dictionary ensuring unique thread names
+    _prefix_thread_counts = dict()
+
     def __init__(self, *args, **kwargs):
+        # if neither name nor prefix is given, use the worker prefix
+        if "name" not in kwargs and "prefix" not in kwargs:
+            kwargs["prefix"] = _WORKER_THREAD_PREFIX
+
+        # if prefix is specified, use it to construct new thread name
+        prefix = kwargs.pop("prefix", None)
+        if prefix:
+            thread_num = self._prefix_thread_counts.get(prefix, 0) + 1
+            self._prefix_thread_counts[prefix] = thread_num
+            kwargs["name"] = prefix + str(thread_num)
+
         threading.Thread.__init__(self, *args, **kwargs)
         self.daemon = True
 
diff --git a/pyanaconda/ui/gui/spokes/datetime_spoke.py b/pyanaconda/ui/gui/spokes/datetime_spoke.py
index b81e888..9376732 100644
--- a/pyanaconda/ui/gui/spokes/datetime_spoke.py
+++ b/pyanaconda/ui/gui/spokes/datetime_spoke.py
@@ -119,9 +119,6 @@ class NTPconfigDialog(GUIObject):
     def __init__(self, *args):
         GUIObject.__init__(self, *args)
 
-        #used to ensure uniqueness of the threads' names
-        self._threads_counter = 0
-
         #epoch is increased when serversStore is repopulated
         self._epoch = 0
         self._epoch_lock = threading.Lock()
@@ -267,11 +264,9 @@ class NTPconfigDialog(GUIObject):
         """ Runs a new thread with _set_server_ok_nok(itr) as a taget. """
 
         self._serversStore.set_value(itr, 1, SERVER_QUERY)
-        new_thread_name = "AnaNTPserver%d" % self._threads_counter
-        threadMgr.add(AnacondaThread(name=new_thread_name,
+        threadMgr.add(AnacondaThread(prefix="AnaNTPserver",
                                      target=self._set_server_ok_nok,
                                      args=(itr, self._epoch)))
-        self._threads_counter += 1
 
     def _add_server(self, server):
         """
diff --git a/pyanaconda/ui/tui/simpleline/base.py b/pyanaconda/ui/tui/simpleline/base.py
index 469c2a0..4e1bbf1 100644
--- a/pyanaconda/ui/tui/simpleline/base.py
+++ b/pyanaconda/ui/tui/simpleline/base.py
@@ -91,9 +91,6 @@ class App(object):
         else:
             self.queue = Queue.Queue()
 
-        # ensure unique thread names
-        self._in_thread_counter = 0
-
         # event handlers
         # key: event id
         # value: list of tuples (callback, data)
@@ -408,10 +405,7 @@ class App(object):
         """This method reads one input from user. Its basic form has only one
         line, but we might need to override it for more complex apps or testing."""
 
-        thread_name = "%s%d" % (constants.THREAD_INPUT_BASENAME,
-                                self._in_thread_counter)
-        self._in_thread_counter += 1
-        input_thread = AnacondaThread(name=thread_name,
+        input_thread = AnacondaThread(prefix=constants.THREAD_INPUT_BASENAME,
                                       target=self._thread_input,
                                       args=(self.queue, prompt, hidden))
         input_thread.daemon = True
-- 
1.8.4.2



More information about the anaconda-patches mailing list