Browse code

use partial(next, itertools.count()) to generate unique ids

Bas Nijholt authored on 22/04/2020 20:14:51
Showing 1 changed files
... ...
@@ -1,7 +1,9 @@
1 1
 import abc
2 2
 import asyncio
3 3
 import concurrent.futures as concurrent
4
+import functools
4 5
 import inspect
6
+import itertools
5 7
 import pickle
6 8
 import sys
7 9
 import time
... ...
@@ -150,9 +152,10 @@ class BaseRunner(metaclass=abc.ABCMeta):
150 152
         self._to_retry = {}
151 153
         self._tracebacks = {}
152 154
 
153
-        # Keeping track of index -> point
154 155
         self._id_to_point = {}
155
-        self._i = 0  # some unique index to be associated with each point
156
+        self._next_id = functools.partial(
157
+            next, itertools.count()
158
+        )  # some unique id to be associated with each point
156 159
 
157 160
     def _get_max_tasks(self):
158 161
         return self._max_tasks or _get_ncores(self.executor)
... ...
@@ -172,10 +175,10 @@ class BaseRunner(metaclass=abc.ABCMeta):
172 175
 
173 176
     def _ask(self, n):
174 177
         points = []
175
-        for i, index in enumerate(self._to_retry.keys()):
178
+        for i, _id in enumerate(self._to_retry.keys()):
176 179
             if i == n:
177 180
                 break
178
-            point = self._id_to_point[index]
181
+            point = self._id_to_point[_id]
179 182
             if point not in self.pending_points.values():
180 183
                 points.append(point)
181 184
 
... ...
@@ -249,11 +252,10 @@ class BaseRunner(metaclass=abc.ABCMeta):
249 252
             fut.start_time = start_time
250 253
             self.pending_points[fut] = x
251 254
             try:
252
-                i = _key_by_value(self._id_to_point, x)  # O(N)
255
+                _id = _key_by_value(self._id_to_point, x)  # O(N)
253 256
             except StopIteration:  # `x` is not a value in `self._id_to_point`
254
-                self._i += 1
255
-                i = self._i
256
-            self._id_to_point[i] = x
257
+                _id = self._next_id()
258
+            self._id_to_point[_id] = x
257 259
 
258 260
         # Collect and results and add them to the learner
259 261
         futures = list(self.pending_points.keys())