_base.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. # Copyright 2009 Brian Quinlan. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. __author__ = 'Brian Quinlan (brian@sweetapp.com)'
  4. import collections
  5. import logging
  6. import threading
  7. import time
  8. FIRST_COMPLETED = 'FIRST_COMPLETED'
  9. FIRST_EXCEPTION = 'FIRST_EXCEPTION'
  10. ALL_COMPLETED = 'ALL_COMPLETED'
  11. _AS_COMPLETED = '_AS_COMPLETED'
  12. # Possible future states (for internal use by the futures package).
  13. PENDING = 'PENDING'
  14. RUNNING = 'RUNNING'
  15. # The future was cancelled by the user...
  16. CANCELLED = 'CANCELLED'
  17. # ...and _Waiter.add_cancelled() was called by a worker.
  18. CANCELLED_AND_NOTIFIED = 'CANCELLED_AND_NOTIFIED'
  19. FINISHED = 'FINISHED'
  20. _FUTURE_STATES = [
  21. PENDING,
  22. RUNNING,
  23. CANCELLED,
  24. CANCELLED_AND_NOTIFIED,
  25. FINISHED
  26. ]
  27. _STATE_TO_DESCRIPTION_MAP = {
  28. PENDING: "pending",
  29. RUNNING: "running",
  30. CANCELLED: "cancelled",
  31. CANCELLED_AND_NOTIFIED: "cancelled",
  32. FINISHED: "finished"
  33. }
  34. # Logger for internal use by the futures package.
  35. LOGGER = logging.getLogger("concurrent.futures")
  36. class Error(Exception):
  37. """Base class for all future-related exceptions."""
  38. pass
  39. class CancelledError(Error):
  40. """The Future was cancelled."""
  41. pass
  42. class TimeoutError(Error):
  43. """The operation exceeded the given deadline."""
  44. pass
  45. class _Waiter(object):
  46. """Provides the event that wait() and as_completed() block on."""
  47. def __init__(self):
  48. self.event = threading.Event()
  49. self.finished_futures = []
  50. def add_result(self, future):
  51. self.finished_futures.append(future)
  52. def add_exception(self, future):
  53. self.finished_futures.append(future)
  54. def add_cancelled(self, future):
  55. self.finished_futures.append(future)
  56. class _AsCompletedWaiter(_Waiter):
  57. """Used by as_completed()."""
  58. def __init__(self):
  59. super(_AsCompletedWaiter, self).__init__()
  60. self.lock = threading.Lock()
  61. def add_result(self, future):
  62. with self.lock:
  63. super(_AsCompletedWaiter, self).add_result(future)
  64. self.event.set()
  65. def add_exception(self, future):
  66. with self.lock:
  67. super(_AsCompletedWaiter, self).add_exception(future)
  68. self.event.set()
  69. def add_cancelled(self, future):
  70. with self.lock:
  71. super(_AsCompletedWaiter, self).add_cancelled(future)
  72. self.event.set()
  73. class _FirstCompletedWaiter(_Waiter):
  74. """Used by wait(return_when=FIRST_COMPLETED)."""
  75. def add_result(self, future):
  76. super().add_result(future)
  77. self.event.set()
  78. def add_exception(self, future):
  79. super().add_exception(future)
  80. self.event.set()
  81. def add_cancelled(self, future):
  82. super().add_cancelled(future)
  83. self.event.set()
  84. class _AllCompletedWaiter(_Waiter):
  85. """Used by wait(return_when=FIRST_EXCEPTION and ALL_COMPLETED)."""
  86. def __init__(self, num_pending_calls, stop_on_exception):
  87. self.num_pending_calls = num_pending_calls
  88. self.stop_on_exception = stop_on_exception
  89. self.lock = threading.Lock()
  90. super().__init__()
  91. def _decrement_pending_calls(self):
  92. with self.lock:
  93. self.num_pending_calls -= 1
  94. if not self.num_pending_calls:
  95. self.event.set()
  96. def add_result(self, future):
  97. super().add_result(future)
  98. self._decrement_pending_calls()
  99. def add_exception(self, future):
  100. super().add_exception(future)
  101. if self.stop_on_exception:
  102. self.event.set()
  103. else:
  104. self._decrement_pending_calls()
  105. def add_cancelled(self, future):
  106. super().add_cancelled(future)
  107. self._decrement_pending_calls()
  108. class _AcquireFutures(object):
  109. """A context manager that does an ordered acquire of Future conditions."""
  110. def __init__(self, futures):
  111. self.futures = sorted(futures, key=id)
  112. def __enter__(self):
  113. for future in self.futures:
  114. future._condition.acquire()
  115. def __exit__(self, *args):
  116. for future in self.futures:
  117. future._condition.release()
  118. def _create_and_install_waiters(fs, return_when):
  119. if return_when == _AS_COMPLETED:
  120. waiter = _AsCompletedWaiter()
  121. elif return_when == FIRST_COMPLETED:
  122. waiter = _FirstCompletedWaiter()
  123. else:
  124. pending_count = sum(
  125. f._state not in [CANCELLED_AND_NOTIFIED, FINISHED] for f in fs)
  126. if return_when == FIRST_EXCEPTION:
  127. waiter = _AllCompletedWaiter(pending_count, stop_on_exception=True)
  128. elif return_when == ALL_COMPLETED:
  129. waiter = _AllCompletedWaiter(pending_count, stop_on_exception=False)
  130. else:
  131. raise ValueError("Invalid return condition: %r" % return_when)
  132. for f in fs:
  133. f._waiters.append(waiter)
  134. return waiter
  135. def _yield_finished_futures(fs, waiter, ref_collect):
  136. """
  137. Iterate on the list *fs*, yielding finished futures one by one in
  138. reverse order.
  139. Before yielding a future, *waiter* is removed from its waiters
  140. and the future is removed from each set in the collection of sets
  141. *ref_collect*.
  142. The aim of this function is to avoid keeping stale references after
  143. the future is yielded and before the iterator resumes.
  144. """
  145. while fs:
  146. f = fs[-1]
  147. for futures_set in ref_collect:
  148. futures_set.remove(f)
  149. with f._condition:
  150. f._waiters.remove(waiter)
  151. del f
  152. # Careful not to keep a reference to the popped value
  153. yield fs.pop()
  154. def as_completed(fs, timeout=None):
  155. """An iterator over the given futures that yields each as it completes.
  156. Args:
  157. fs: The sequence of Futures (possibly created by different Executors) to
  158. iterate over.
  159. timeout: The maximum number of seconds to wait. If None, then there
  160. is no limit on the wait time.
  161. Returns:
  162. An iterator that yields the given Futures as they complete (finished or
  163. cancelled). If any given Futures are duplicated, they will be returned
  164. once.
  165. Raises:
  166. TimeoutError: If the entire result iterator could not be generated
  167. before the given timeout.
  168. """
  169. if timeout is not None:
  170. end_time = timeout + time.monotonic()
  171. fs = set(fs)
  172. total_futures = len(fs)
  173. with _AcquireFutures(fs):
  174. finished = set(
  175. f for f in fs
  176. if f._state in [CANCELLED_AND_NOTIFIED, FINISHED])
  177. pending = fs - finished
  178. waiter = _create_and_install_waiters(fs, _AS_COMPLETED)
  179. finished = list(finished)
  180. try:
  181. yield from _yield_finished_futures(finished, waiter,
  182. ref_collect=(fs,))
  183. while pending:
  184. if timeout is None:
  185. wait_timeout = None
  186. else:
  187. wait_timeout = end_time - time.monotonic()
  188. if wait_timeout < 0:
  189. raise TimeoutError(
  190. '%d (of %d) futures unfinished' % (
  191. len(pending), total_futures))
  192. waiter.event.wait(wait_timeout)
  193. with waiter.lock:
  194. finished = waiter.finished_futures
  195. waiter.finished_futures = []
  196. waiter.event.clear()
  197. # reverse to keep finishing order
  198. finished.reverse()
  199. yield from _yield_finished_futures(finished, waiter,
  200. ref_collect=(fs, pending))
  201. finally:
  202. # Remove waiter from unfinished futures
  203. for f in fs:
  204. with f._condition:
  205. f._waiters.remove(waiter)
  206. DoneAndNotDoneFutures = collections.namedtuple(
  207. 'DoneAndNotDoneFutures', 'done not_done')
  208. def wait(fs, timeout=None, return_when=ALL_COMPLETED):
  209. """Wait for the futures in the given sequence to complete.
  210. Args:
  211. fs: The sequence of Futures (possibly created by different Executors) to
  212. wait upon.
  213. timeout: The maximum number of seconds to wait. If None, then there
  214. is no limit on the wait time.
  215. return_when: Indicates when this function should return. The options
  216. are:
  217. FIRST_COMPLETED - Return when any future finishes or is
  218. cancelled.
  219. FIRST_EXCEPTION - Return when any future finishes by raising an
  220. exception. If no future raises an exception
  221. then it is equivalent to ALL_COMPLETED.
  222. ALL_COMPLETED - Return when all futures finish or are cancelled.
  223. Returns:
  224. A named 2-tuple of sets. The first set, named 'done', contains the
  225. futures that completed (is finished or cancelled) before the wait
  226. completed. The second set, named 'not_done', contains uncompleted
  227. futures.
  228. """
  229. with _AcquireFutures(fs):
  230. done = set(f for f in fs
  231. if f._state in [CANCELLED_AND_NOTIFIED, FINISHED])
  232. not_done = set(fs) - done
  233. if (return_when == FIRST_COMPLETED) and done:
  234. return DoneAndNotDoneFutures(done, not_done)
  235. elif (return_when == FIRST_EXCEPTION) and done:
  236. if any(f for f in done
  237. if not f.cancelled() and f.exception() is not None):
  238. return DoneAndNotDoneFutures(done, not_done)
  239. if len(done) == len(fs):
  240. return DoneAndNotDoneFutures(done, not_done)
  241. waiter = _create_and_install_waiters(fs, return_when)
  242. waiter.event.wait(timeout)
  243. for f in fs:
  244. with f._condition:
  245. f._waiters.remove(waiter)
  246. done.update(waiter.finished_futures)
  247. return DoneAndNotDoneFutures(done, set(fs) - done)
  248. class Future(object):
  249. """Represents the result of an asynchronous computation."""
  250. def __init__(self):
  251. """Initializes the future. Should not be called by clients."""
  252. self._condition = threading.Condition()
  253. self._state = PENDING
  254. self._result = None
  255. self._exception = None
  256. self._waiters = []
  257. self._done_callbacks = []
  258. def _invoke_callbacks(self):
  259. for callback in self._done_callbacks:
  260. try:
  261. callback(self)
  262. except Exception:
  263. LOGGER.exception('exception calling callback for %r', self)
  264. def __repr__(self):
  265. with self._condition:
  266. if self._state == FINISHED:
  267. if self._exception:
  268. return '<%s at %#x state=%s raised %s>' % (
  269. self.__class__.__name__,
  270. id(self),
  271. _STATE_TO_DESCRIPTION_MAP[self._state],
  272. self._exception.__class__.__name__)
  273. else:
  274. return '<%s at %#x state=%s returned %s>' % (
  275. self.__class__.__name__,
  276. id(self),
  277. _STATE_TO_DESCRIPTION_MAP[self._state],
  278. self._result.__class__.__name__)
  279. return '<%s at %#x state=%s>' % (
  280. self.__class__.__name__,
  281. id(self),
  282. _STATE_TO_DESCRIPTION_MAP[self._state])
  283. def cancel(self):
  284. """Cancel the future if possible.
  285. Returns True if the future was cancelled, False otherwise. A future
  286. cannot be cancelled if it is running or has already completed.
  287. """
  288. with self._condition:
  289. if self._state in [RUNNING, FINISHED]:
  290. return False
  291. if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
  292. return True
  293. self._state = CANCELLED
  294. self._condition.notify_all()
  295. self._invoke_callbacks()
  296. return True
  297. def cancelled(self):
  298. """Return True if the future was cancelled."""
  299. with self._condition:
  300. return self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]
  301. def running(self):
  302. """Return True if the future is currently executing."""
  303. with self._condition:
  304. return self._state == RUNNING
  305. def done(self):
  306. """Return True of the future was cancelled or finished executing."""
  307. with self._condition:
  308. return self._state in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]
  309. def __get_result(self):
  310. if self._exception:
  311. raise self._exception
  312. else:
  313. return self._result
  314. def add_done_callback(self, fn):
  315. """Attaches a callable that will be called when the future finishes.
  316. Args:
  317. fn: A callable that will be called with this future as its only
  318. argument when the future completes or is cancelled. The callable
  319. will always be called by a thread in the same process in which
  320. it was added. If the future has already completed or been
  321. cancelled then the callable will be called immediately. These
  322. callables are called in the order that they were added.
  323. """
  324. with self._condition:
  325. if self._state not in [CANCELLED, CANCELLED_AND_NOTIFIED, FINISHED]:
  326. self._done_callbacks.append(fn)
  327. return
  328. try:
  329. fn(self)
  330. except Exception:
  331. LOGGER.exception('exception calling callback for %r', self)
  332. def result(self, timeout=None):
  333. """Return the result of the call that the future represents.
  334. Args:
  335. timeout: The number of seconds to wait for the result if the future
  336. isn't done. If None, then there is no limit on the wait time.
  337. Returns:
  338. The result of the call that the future represents.
  339. Raises:
  340. CancelledError: If the future was cancelled.
  341. TimeoutError: If the future didn't finish executing before the given
  342. timeout.
  343. Exception: If the call raised then that exception will be raised.
  344. """
  345. with self._condition:
  346. if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
  347. raise CancelledError()
  348. elif self._state == FINISHED:
  349. return self.__get_result()
  350. self._condition.wait(timeout)
  351. if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
  352. raise CancelledError()
  353. elif self._state == FINISHED:
  354. return self.__get_result()
  355. else:
  356. raise TimeoutError()
  357. def exception(self, timeout=None):
  358. """Return the exception raised by the call that the future represents.
  359. Args:
  360. timeout: The number of seconds to wait for the exception if the
  361. future isn't done. If None, then there is no limit on the wait
  362. time.
  363. Returns:
  364. The exception raised by the call that the future represents or None
  365. if the call completed without raising.
  366. Raises:
  367. CancelledError: If the future was cancelled.
  368. TimeoutError: If the future didn't finish executing before the given
  369. timeout.
  370. """
  371. with self._condition:
  372. if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
  373. raise CancelledError()
  374. elif self._state == FINISHED:
  375. return self._exception
  376. self._condition.wait(timeout)
  377. if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
  378. raise CancelledError()
  379. elif self._state == FINISHED:
  380. return self._exception
  381. else:
  382. raise TimeoutError()
  383. # The following methods should only be used by Executors and in tests.
  384. def set_running_or_notify_cancel(self):
  385. """Mark the future as running or process any cancel notifications.
  386. Should only be used by Executor implementations and unit tests.
  387. If the future has been cancelled (cancel() was called and returned
  388. True) then any threads waiting on the future completing (though calls
  389. to as_completed() or wait()) are notified and False is returned.
  390. If the future was not cancelled then it is put in the running state
  391. (future calls to running() will return True) and True is returned.
  392. This method should be called by Executor implementations before
  393. executing the work associated with this future. If this method returns
  394. False then the work should not be executed.
  395. Returns:
  396. False if the Future was cancelled, True otherwise.
  397. Raises:
  398. RuntimeError: if this method was already called or if set_result()
  399. or set_exception() was called.
  400. """
  401. with self._condition:
  402. if self._state == CANCELLED:
  403. self._state = CANCELLED_AND_NOTIFIED
  404. for waiter in self._waiters:
  405. waiter.add_cancelled(self)
  406. # self._condition.notify_all() is not necessary because
  407. # self.cancel() triggers a notification.
  408. return False
  409. elif self._state == PENDING:
  410. self._state = RUNNING
  411. return True
  412. else:
  413. LOGGER.critical('Future %s in unexpected state: %s',
  414. id(self),
  415. self._state)
  416. raise RuntimeError('Future in unexpected state')
  417. def set_result(self, result):
  418. """Sets the return value of work associated with the future.
  419. Should only be used by Executor implementations and unit tests.
  420. """
  421. with self._condition:
  422. self._result = result
  423. self._state = FINISHED
  424. for waiter in self._waiters:
  425. waiter.add_result(self)
  426. self._condition.notify_all()
  427. self._invoke_callbacks()
  428. def set_exception(self, exception):
  429. """Sets the result of the future as being the given exception.
  430. Should only be used by Executor implementations and unit tests.
  431. """
  432. with self._condition:
  433. self._exception = exception
  434. self._state = FINISHED
  435. for waiter in self._waiters:
  436. waiter.add_exception(self)
  437. self._condition.notify_all()
  438. self._invoke_callbacks()
  439. class Executor(object):
  440. """This is an abstract base class for concrete asynchronous executors."""
  441. def submit(*args, **kwargs):
  442. """Submits a callable to be executed with the given arguments.
  443. Schedules the callable to be executed as fn(*args, **kwargs) and returns
  444. a Future instance representing the execution of the callable.
  445. Returns:
  446. A Future representing the given call.
  447. """
  448. if len(args) >= 2:
  449. pass
  450. elif not args:
  451. raise TypeError("descriptor 'submit' of 'Executor' object "
  452. "needs an argument")
  453. elif 'fn' not in kwargs:
  454. raise TypeError('submit expected at least 1 positional argument, '
  455. 'got %d' % (len(args)-1))
  456. raise NotImplementedError()
  457. def map(self, fn, *iterables, timeout=None, chunksize=1):
  458. """Returns an iterator equivalent to map(fn, iter).
  459. Args:
  460. fn: A callable that will take as many arguments as there are
  461. passed iterables.
  462. timeout: The maximum number of seconds to wait. If None, then there
  463. is no limit on the wait time.
  464. chunksize: The size of the chunks the iterable will be broken into
  465. before being passed to a child process. This argument is only
  466. used by ProcessPoolExecutor; it is ignored by
  467. ThreadPoolExecutor.
  468. Returns:
  469. An iterator equivalent to: map(func, *iterables) but the calls may
  470. be evaluated out-of-order.
  471. Raises:
  472. TimeoutError: If the entire result iterator could not be generated
  473. before the given timeout.
  474. Exception: If fn(*args) raises for any values.
  475. """
  476. if timeout is not None:
  477. end_time = timeout + time.monotonic()
  478. fs = [self.submit(fn, *args) for args in zip(*iterables)]
  479. # Yield must be hidden in closure so that the futures are submitted
  480. # before the first iterator value is required.
  481. def result_iterator():
  482. try:
  483. # reverse to keep finishing order
  484. fs.reverse()
  485. while fs:
  486. # Careful not to keep a reference to the popped future
  487. if timeout is None:
  488. yield fs.pop().result()
  489. else:
  490. yield fs.pop().result(end_time - time.monotonic())
  491. finally:
  492. for future in fs:
  493. future.cancel()
  494. return result_iterator()
  495. def shutdown(self, wait=True):
  496. """Clean-up the resources associated with the Executor.
  497. It is safe to call this method several times. Otherwise, no other
  498. methods can be called after this one.
  499. Args:
  500. wait: If True then shutdown will not return until all running
  501. futures have finished executing and the resources used by the
  502. executor have been reclaimed.
  503. """
  504. pass
  505. def __enter__(self):
  506. return self
  507. def __exit__(self, exc_type, exc_val, exc_tb):
  508. self.shutdown(wait=True)
  509. return False
  510. class BrokenExecutor(RuntimeError):
  511. """
  512. Raised when a executor has become non-functional after a severe failure.
  513. """