util/guid.py merged into ec.py that is the only one using it
[nepi.git] / src / nepi / execution / ec.py
index c403d79..f282ac6 100644 (file)
@@ -18,7 +18,6 @@
 
 from six import next
 
-from nepi.util import guid
 from nepi.util.parallel import ParallelRun
 from nepi.util.timefuncs import tnow, tdiffsec, stabsformat, tsformat 
 from nepi.execution.resource import ResourceFactory, ResourceAction, \
@@ -103,6 +102,23 @@ class ECState(object):
     RELEASED = 3
     TERMINATED = 4
 
+# historical note: this class used to be in util/guid.py but is used only here
+# FIXME: This class is not thread-safe. Should it be made thread-safe?
+class GuidGenerator(object):
+    def __init__(self):
+        self._last_guid = 0
+
+    # historical note: this used to be called `next`
+    # which confused 2to3 - and me - while it has
+    # nothing to do at all with the iteration protocol
+    def generate(self, guid = None):
+        if guid == None:
+            guid = self._last_guid + 1
+
+        self._last_guid = self._last_guid if guid <= self._last_guid else guid
+
+        return guid
+
 class ExperimentController(object):
     """
     .. note::
@@ -225,7 +241,7 @@ class ExperimentController(object):
         self._persist = persist
 
         # generator of globally unique ids
-        self._guid_generator = guid.GuidGenerator()
+        self._guid_generator = GuidGenerator()
         
         # Resource managers
         self._resources = dict()
@@ -242,7 +258,7 @@ class ExperimentController(object):
         self._groups = dict()
 
         # generator of globally unique id for groups
-        self._group_id_generator = guid.GuidGenerator()
+        self._group_id_generator = GuidGenerator()
 
         # Flag to stop processing thread
         self._stop = False
@@ -590,7 +606,7 @@ class ExperimentController(object):
         """
         # Get next available guid
         # xxx_next_hiccup
-        guid = self._guid_generator.next(guid)
+        guid = self._guid_generator.generate(guid)
         
         # Instantiate RM
         rm = ResourceFactory.create(rtype, self, guid)
@@ -989,7 +1005,7 @@ class ExperimentController(object):
         if not group:
             new_group = True
             # xxx_next_hiccup
-            group = self._group_id_generator.next()
+            group = self._group_id_generator.generate()
 
         if group not in self._groups:
             self._groups[group] = []