aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/python/src/grpc/framework/foundation/_timer_future.py
blob: 2c9996aa9db7ab79172586a61b0b7af9a9f6cab3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Copyright 2015, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""Affords a Future implementation based on Python's threading.Timer."""

import sys
import threading
import time

from grpc.framework.foundation import future


class TimerFuture(future.Future):
  """A Future implementation based around Timer objects."""

  def __init__(self, compute_time, computation):
    """Constructor.

    Args:
      compute_time: The time after which to begin this future's computation.
      computation: The computation to be performed within this Future.
    """
    self._lock = threading.Lock()
    self._compute_time = compute_time
    self._computation = computation
    self._timer = None
    self._computing = False
    self._computed = False
    self._cancelled = False
    self._result = None
    self._exception = None
    self._traceback = None
    self._waiting = []

  def _compute(self):
    """Performs the computation embedded in this Future.

    Or doesn't, if the time to perform it has not yet arrived.
    """
    with self._lock:
      time_remaining = self._compute_time - time.time()
      if 0 < time_remaining:
        self._timer = threading.Timer(time_remaining, self._compute)
        self._timer.start()
        return
      else:
        self._computing = True

    try:
      return_value = self._computation()
      exception = None
      traceback = None
    except Exception as e:  # pylint: disable=broad-except
      return_value = None
      exception = e
      traceback = sys.exc_info()[2]

    with self._lock:
      self._computing = False
      self._computed = True
      self._return_value = return_value
      self._exception = exception
      self._traceback = traceback
      waiting = self._waiting

    for callback in waiting:
      callback(self)

  def start(self):
    """Starts this Future.

    This must be called exactly once, immediately after construction.
    """
    with self._lock:
      self._timer = threading.Timer(
          self._compute_time - time.time(), self._compute)
      self._timer.start()

  def cancel(self):
    """See future.Future.cancel for specification."""
    with self._lock:
      if self._computing or self._computed:
        return False
      elif self._cancelled:
        return True
      else:
        self._timer.cancel()
        self._cancelled = True
        waiting = self._waiting

    for callback in waiting:
      try:
        callback(self)
      except Exception:  # pylint: disable=broad-except
        pass

    return True

  def cancelled(self):
    """See future.Future.cancelled for specification."""
    with self._lock:
      return self._cancelled

  def running(self):
    """See future.Future.running for specification."""
    with self._lock:
      return not self._computed and not self._cancelled

  def done(self):
    """See future.Future.done for specification."""
    with self._lock:
      return self._computed or self._cancelled

  def result(self, timeout=None):
    """See future.Future.result for specification."""
    with self._lock:
      if self._cancelled:
        raise future.CancelledError()
      elif self._computed:
        if self._exception is None:
          return self._return_value
        else:
          raise self._exception  # pylint: disable=raising-bad-type

      condition = threading.Condition()
      def notify_condition(unused_future):
        with condition:
          condition.notify()
      self._waiting.append(notify_condition)

    with condition:
      condition.wait(timeout=timeout)

    with self._lock:
      if self._cancelled:
        raise future.CancelledError()
      elif self._computed:
        if self._exception is None:
          return self._return_value
        else:
          raise self._exception  # pylint: disable=raising-bad-type
      else:
        raise future.TimeoutError()

  def exception(self, timeout=None):
    """See future.Future.exception for specification."""
    with self._lock:
      if self._cancelled:
        raise future.CancelledError()
      elif self._computed:
        return self._exception

      condition = threading.Condition()
      def notify_condition(unused_future):
        with condition:
          condition.notify()
      self._waiting.append(notify_condition)

    with condition:
      condition.wait(timeout=timeout)

    with self._lock:
      if self._cancelled:
        raise future.CancelledError()
      elif self._computed:
        return self._exception
      else:
        raise future.TimeoutError()

  def traceback(self, timeout=None):
    """See future.Future.traceback for specification."""
    with self._lock:
      if self._cancelled:
        raise future.CancelledError()
      elif self._computed:
        return self._traceback

      condition = threading.Condition()
      def notify_condition(unused_future):
        with condition:
          condition.notify()
      self._waiting.append(notify_condition)

    with condition:
      condition.wait(timeout=timeout)

    with self._lock:
      if self._cancelled:
        raise future.CancelledError()
      elif self._computed:
        return self._traceback
      else:
        raise future.TimeoutError()

  def add_done_callback(self, fn):
    """See future.Future.add_done_callback for specification."""
    with self._lock:
      if not self._computed and not self._cancelled:
        self._waiting.append(fn)
        return

    fn(self)