| 283 | return inf |
| 284 | |
| 285 | def _prune(self) -> None: |
| 286 | # In principle, it's possible for a cancel scope to toggle back and |
| 287 | # forth repeatedly between the same two deadlines, and end up with |
| 288 | # lots of stale entries that *look* like they're still active, because |
| 289 | # their deadline is correct, but in fact are redundant. So when |
| 290 | # pruning we have to eliminate entries with the wrong deadline, *and* |
| 291 | # eliminate duplicates. |
| 292 | seen = set() |
| 293 | pruned_heap = [] |
| 294 | for deadline, tiebreaker, cancel_scope in self._heap: |
| 295 | if deadline == cancel_scope._registered_deadline: |
| 296 | if cancel_scope in seen: |
| 297 | continue |
| 298 | seen.add(cancel_scope) |
| 299 | pruned_heap.append((deadline, tiebreaker, cancel_scope)) |
| 300 | # See test_cancel_scope_deadline_duplicates for a test that exercises |
| 301 | # this assert: |
| 302 | assert len(pruned_heap) == self._active |
| 303 | heapify(pruned_heap) |
| 304 | self._heap = pruned_heap |
| 305 | |
| 306 | def expire(self, now: float) -> bool: |
| 307 | did_something = False |