Lines Matching refs:self
61 def __init__(self):
62 self.event = threading.Event()
63 self.finished_futures = []
65 def add_result(self, future):
66 self.finished_futures.append(future)
68 def add_exception(self, future):
69 self.finished_futures.append(future)
71 def add_cancelled(self, future):
72 self.finished_futures.append(future)
77 def __init__(self):
78 super(_AsCompletedWaiter, self).__init__()
79 self.lock = threading.Lock()
81 def add_result(self, future):
82 with self.lock:
83 super(_AsCompletedWaiter, self).add_result(future)
84 self.event.set()
86 def add_exception(self, future):
87 with self.lock:
88 super(_AsCompletedWaiter, self).add_exception(future)
89 self.event.set()
91 def add_cancelled(self, future):
92 with self.lock:
93 super(_AsCompletedWaiter, self).add_cancelled(future)
94 self.event.set()
99 def add_result(self, future):
101 self.event.set()
103 def add_exception(self, future):
105 self.event.set()
107 def add_cancelled(self, future):
109 self.event.set()
114 def __init__(self, num_pending_calls, stop_on_exception):
115 self.num_pending_calls = num_pending_calls
116 self.stop_on_exception = stop_on_exception
117 self.lock = threading.Lock()
120 def _decrement_pending_calls(self):
121 with self.lock:
122 self.num_pending_calls -= 1
123 if not self.num_pending_calls:
124 self.event.set()
126 def add_result(self, future):
128 self._decrement_pending_calls()
130 def add_exception(self, future):
132 if self.stop_on_exception:
133 self.event.set()
135 self._decrement_pending_calls()
137 def add_cancelled(self, future):
139 self._decrement_pending_calls()
144 def __init__(self, futures):
145 self.futures = sorted(futures, key=id)
147 def __enter__(self):
148 for future in self.futures:
151 def __exit__(self, *args):
152 for future in self.futures:
321 # Break a reference cycle with the exception in self._exception
328 def __init__(self):
330 self._condition = threading.Condition()
331 self._state = PENDING
332 self._result = None
333 self._exception = None
334 self._waiters = []
335 self._done_callbacks = []
337 def _invoke_callbacks(self):
338 for callback in self._done_callbacks:
340 callback(self)
342 LOGGER.exception('exception calling callback for %r', self)
344 def __repr__(self):
345 with self._condition:
346 if self._state == FINISHED:
347 if self._exception:
349 self.__class__.__name__,
350 id(self),
351 _STATE_TO_DESCRIPTION_MAP[self._state],
352 self._exception.__class__.__name__)
355 self.__class__.__name__,
356 id(self),
357 _STATE_TO_DESCRIPTION_MAP[self._state],
358 self._result.__class__.__name__)
360 self.__class__.__name__,
361 id(self),
362 _STATE_TO_DESCRIPTION_MAP[self._state])
364 def cancel(self):
370 with self._condition:
371 if self._state in [RUNNING, FINISHED]:
374 if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
377 self._state = CANCELLED
378 self._condition.notify_all()
380 self._invoke_callbacks()
383 def cancelled(self):
385 with self._condition:
386 return self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]
388 def running(self):
390 with self._condition:
391 return self._state == RUNNING
393 def done(self):
395 with self._condition:
396 return self._state in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]
398 def __get_result(self):
399 if self._exception:
401 raise self._exception
403 # Break a reference cycle with the exception in self._exception
404 self = None
406 return self._result
408 def add_done_callback(self, fn):
419 with self._condition:
420 if self._state not in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]:
421 self._done_callbacks.append(fn)
424 fn(self)
426 LOGGER.exception('exception calling callback for %r', self)
428 def result(self, timeout=None):
445 with self._condition:
446 if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
448 elif self._state == FINISHED:
449 return self.__get_result()
451 self._condition.wait(timeout)
453 if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
455 elif self._state == FINISHED:
456 return self.__get_result()
460 # Break a reference cycle with the exception in self._exception
461 self = None
463 def exception(self, timeout=None):
481 with self._condition:
482 if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
484 elif self._state == FINISHED:
485 return self._exception
487 self._condition.wait(timeout)
489 if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
491 elif self._state == FINISHED:
492 return self._exception
497 def set_running_or_notify_cancel(self):
520 with self._condition:
521 if self._state == CANCELLED:
522 self._state = CANCELLED_AND_NOTIFIED
523 for waiter in self._waiters:
524 waiter.add_cancelled(self)
525 # self._condition.notify_all() is not necessary because
526 # self.cancel() triggers a notification.
528 elif self._state == PENDING:
529 self._state = RUNNING
533 id(self),
534 self._state)
537 def set_result(self, result):
542 with self._condition:
543 if self._state in {CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED}:
544 raise InvalidStateError('{}: {!r}'.format(self._state, self))
545 self._result = result
546 self._state = FINISHED
547 for waiter in self._waiters:
548 waiter.add_result(self)
549 self._condition.notify_all()
550 self._invoke_callbacks()
552 def set_exception(self, exception):
557 with self._condition:
558 if self._state in {CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED}:
559 raise InvalidStateError('{}: {!r}'.format(self._state, self))
560 self._exception = exception
561 self._state = FINISHED
562 for waiter in self._waiters:
563 waiter.add_exception(self)
564 self._condition.notify_all()
565 self._invoke_callbacks()
572 def submit(self, fn, /, *args, **kwargs):
583 def map(self, fn, *iterables, timeout=None, chunksize=1):
608 fs = [self.submit(fn, *args) for args in zip(*iterables)]
627 def shutdown(self, wait=True, *, cancel_futures=False):
643 def __enter__(self):
644 return self
646 def __exit__(self, exc_type, exc_val, exc_tb):
647 self.shutdown(wait=True)