An iterator over the given futures that yields each as it completes. Args: fs: The sequence of Futures (possibly created by different Executors) to iterate over. timeout: The maximum number of seconds to wait. If None, then there is no limit on the wait t
(fs, timeout=None)
| 191 | |
| 192 | |
| 193 | def as_completed(fs, timeout=None): |
| 194 | """An iterator over the given futures that yields each as it completes. |
| 195 | |
| 196 | Args: |
| 197 | fs: The sequence of Futures (possibly created by different Executors) to |
| 198 | iterate over. |
| 199 | timeout: The maximum number of seconds to wait. If None, then there |
| 200 | is no limit on the wait time. |
| 201 | |
| 202 | Returns: |
| 203 | An iterator that yields the given Futures as they complete (finished or |
| 204 | cancelled). If any given Futures are duplicated, they will be returned |
| 205 | once. |
| 206 | |
| 207 | Raises: |
| 208 | TimeoutError: If the entire result iterator could not be generated |
| 209 | before the given timeout. |
| 210 | """ |
| 211 | if timeout is not None: |
| 212 | end_time = timeout + time.monotonic() |
| 213 | |
| 214 | fs = set(fs) |
| 215 | total_futures = len(fs) |
| 216 | with _AcquireFutures(fs): |
| 217 | finished = set( |
| 218 | f for f in fs |
| 219 | if f._state in [CANCELLED_AND_NOTIFIED, FINISHED]) |
| 220 | pending = fs - finished |
| 221 | waiter = _create_and_install_waiters(fs, _AS_COMPLETED) |
| 222 | finished = list(finished) |
| 223 | try: |
| 224 | yield from _yield_finished_futures(finished, waiter, |
| 225 | ref_collect=(fs,)) |
| 226 | |
| 227 | while pending: |
| 228 | if timeout is None: |
| 229 | wait_timeout = None |
| 230 | else: |
| 231 | wait_timeout = end_time - time.monotonic() |
| 232 | if wait_timeout < 0: |
| 233 | raise TimeoutError( |
| 234 | '%d (of %d) futures unfinished' % ( |
| 235 | len(pending), total_futures)) |
| 236 | |
| 237 | waiter.event.wait(wait_timeout) |
| 238 | |
| 239 | with waiter.lock: |
| 240 | finished = waiter.finished_futures |
| 241 | waiter.finished_futures = [] |
| 242 | waiter.event.clear() |
| 243 | |
| 244 | # reverse to keep finishing order |
| 245 | finished.reverse() |
| 246 | yield from _yield_finished_futures(finished, waiter, |
| 247 | ref_collect=(fs, pending)) |
| 248 | |
| 249 | finally: |
| 250 | # Remove waiter from unfinished futures |
nothing calls this directly
no test coverage detected
searching dependent graphs…