| 160 | |
| 161 | |
| 162 | class PoolLocker(ExceptionCatchingTask): |
| 163 | def __init__(self, pool): |
| 164 | super().__init__(target=self.lock_pool) |
| 165 | self.pool = pool |
| 166 | self.daemon = True |
| 167 | self.locked = create_event() |
| 168 | self.unlock = create_event() |
| 169 | |
| 170 | def lock_pool(self): |
| 171 | with self.pool.lock: |
| 172 | self.locked.set() |
| 173 | # Wait for the unlock flag. |
| 174 | unlock_pool = self.wait(self.unlock, 10) |
| 175 | if not unlock_pool: |
| 176 | raise Exception("timed out waiting for unlock signal: deadlock?") |
| 177 | |
| 178 | def wait(self, event: Event, timeout: int): |
| 179 | if _IS_SYNC: |
| 180 | return event.wait(timeout) # type: ignore[call-arg] |
| 181 | else: |
| 182 | try: |
| 183 | asyncio.wait_for(event.wait(), timeout=timeout) |
| 184 | except asyncio.TimeoutError: |
| 185 | return False |
| 186 | return True |
| 187 | |
| 188 | |
| 189 | if __name__ == "__main__": |
no outgoing calls