| 326 | # |
| 327 | |
| 328 | class Event(object): |
| 329 | |
| 330 | def __init__(self, *, ctx): |
| 331 | self._cond = ctx.Condition(ctx.Lock()) |
| 332 | self._flag = ctx.Semaphore(0) |
| 333 | |
| 334 | def is_set(self): |
| 335 | with self._cond: |
| 336 | if self._flag.acquire(False): |
| 337 | self._flag.release() |
| 338 | return True |
| 339 | return False |
| 340 | |
| 341 | def set(self): |
| 342 | with self._cond: |
| 343 | self._flag.acquire(False) |
| 344 | self._flag.release() |
| 345 | self._cond.notify_all() |
| 346 | |
| 347 | def clear(self): |
| 348 | with self._cond: |
| 349 | self._flag.acquire(False) |
| 350 | |
| 351 | def wait(self, timeout=None): |
| 352 | with self._cond: |
| 353 | if self._flag.acquire(False): |
| 354 | self._flag.release() |
| 355 | else: |
| 356 | self._cond.wait(timeout) |
| 357 | |
| 358 | if self._flag.acquire(False): |
| 359 | self._flag.release() |
| 360 | return True |
| 361 | return False |
| 362 | |
| 363 | def __repr__(self): |
| 364 | set_status = 'set' if self.is_set() else 'unset' |
| 365 | return f"<{type(self).__qualname__} at {id(self):#x} {set_status}>" |
| 366 | # |
| 367 | # Barrier |
| 368 | # |