44import time
55from abc import ABC , abstractmethod , abstractproperty
66from concurrent .futures import Future
7- from typing import (
8- Callable ,
9- Generic ,
10- Mapping ,
11- Optional ,
12- Protocol ,
13- Sequence ,
14- TypeVar ,
15- Union ,
16- )
7+ from typing import Callable , Generic , Mapping , Optional , Sequence , TypeVar , Union
178
189from arroyo .types import BrokerValue , Partition , Topic , TStrategyPayload
1910
@@ -187,32 +178,11 @@ def member_id(self) -> str:
187178 raise NotImplementedError
188179
189180
190- class ProducerFuture (Protocol , Generic [T ]):
191- """
192- An abstract interface for a kind of Future. Stdlib futures are too slow to
193- construct, so we use these.
194- """
195-
196- def done (self ) -> bool :
197- ...
198-
199- def result (self , timeout : float | None = None ) -> T :
200- """
201- Return result or raise exception. May block, but does not have to.
202- """
203- ...
204-
205- def set_result (self , result : T ) -> None :
206- ...
207-
208- def set_exception (self , exception : Exception ) -> None :
209- ...
210-
211-
212181class SimpleProducerFuture (Generic [T ]):
213182 """
214183 A stub for concurrent.futures.Future that does not construct any Condition
215- variables, therefore is faster to construct.
184+ variables, therefore is faster to construct. However, some methods are
185+ missing, and result() in particular is not efficient with timeout > 0.
216186 """
217187
218188 def __init__ (self ) -> None :
@@ -232,6 +202,10 @@ def result(self, timeout: float | None = None) -> T:
232202 # only in tests at most. It is only here for the sake of implementing
233203 # the contract. If you really need result with timeout>0, you should
234204 # use the stdlib future.
205+ #
206+ # If this becomes performance sensitive, we can potentially implement
207+ # something more sophisticated such as lazily creating the condition
208+ # variable, and synchronizing the creation of that using a global lock.
235209 while deadline is None or time .time () < deadline :
236210 if self .result_exception is not None :
237211 raise self .result_exception
@@ -248,6 +222,9 @@ def set_exception(self, exception: Exception) -> None:
248222 self .result_exception = exception
249223
250224
225+ ProducerFuture = Union [SimpleProducerFuture [T ], Future [T ]]
226+
227+
251228class Producer (Generic [TStrategyPayload ], ABC ):
252229 @abstractmethod
253230 def produce (
0 commit comments