|
28 | 28 | from google.cloud.ndb import key as key_module |
29 | 29 |
|
30 | 30 |
|
31 | | -def _generate_context_ids(): |
32 | | - """Generate a sequence of context ids. |
| 31 | +class _ContextIds: |
| 32 | + """Iterator which generates a sequence of context ids. |
33 | 33 |
|
34 | 34 | Useful for debugging complicated interactions among concurrent processes and |
35 | 35 | threads. |
36 | 36 |
|
37 | | - The return value is a generator for strings that include the machine's "node", |
38 | | - acquired via `uuid.getnode()`, the current process id, and a sequence number which |
39 | | - increases monotonically starting from one in each process. The combination of all |
40 | | - three is sufficient to uniquely identify the context in which a particular piece of |
41 | | - code is being run. Each context, as it is created, is assigned the next id in this |
42 | | - sequence. The context id is used by `utils.logging_debug` to grant insight into |
43 | | - where a debug logging statement is coming from in a cloud evironment. |
44 | | -
|
45 | | - Returns: |
46 | | - Generator[str]: Sequence of context ids. |
| 37 | + Each value in the sequence is a string that include the machine's "node", acquired |
| 38 | + via `uuid.getnode()`, the current process id, and a sequence number which increases |
| 39 | + monotonically starting from one in each process. The combination of all three is |
| 40 | + sufficient to uniquely identify the context in which a particular piece of code is |
| 41 | + being run. Each context, as it is created, is assigned the next id in this sequence. |
| 42 | + The context id is used by `utils.logging_debug` to grant insight into where a debug |
| 43 | + logging statement is coming from in a cloud evironment. |
47 | 44 | """ |
48 | | - prefix = "{}-{}-".format(uuid.getnode(), os.getpid()) |
49 | | - for sequence_number in itertools.count(1): # pragma NO BRANCH |
50 | | - # pragma is required because this loop never exits (infinite sequence) |
51 | | - yield prefix + str(sequence_number) |
| 45 | + |
| 46 | + def __init__(self): |
| 47 | + self.prefix = "{}-{}-".format(uuid.getnode(), os.getpid()) |
| 48 | + self.counter = itertools.count(1) |
| 49 | + self.lock = threading.Lock() |
| 50 | + |
| 51 | + def __next__(self): |
| 52 | + with self.lock: |
| 53 | + sequence_number = next(self.counter) |
| 54 | + |
| 55 | + return self.prefix + str(sequence_number) |
| 56 | + |
| 57 | + next = __next__ # Python 2.7 |
52 | 58 |
|
53 | 59 |
|
54 | | -_context_ids = _generate_context_ids() |
| 60 | +_context_ids = _ContextIds() |
55 | 61 |
|
56 | 62 |
|
57 | 63 | try: # pragma: NO PY2 COVER |
|
0 commit comments