Why is asyncio.Future incompatible with concurrent.futures.Future?

The two classes are great abstractions for concurrent programming, so it's a little confusing that they do not support the same API.

In particular, according to docs :

asyncio.Futurealmost compatible with concurrent.futures.Future.

Differences:

  • result()and exception()they don’t accept the timeout argument and throw an exception when the future is not yet fulfilled.
  • Callbacks registered with add_done_callback()are always called through an event loop call_soon_threadsafe().
  • This class is not compatible with the functions wait()and as_completed()in the package concurrent.futures.

The above list is actually incomplete, there are a few more differences:

  • running() missing method
  • result()and exception()can raise InvalidStateErrorif too early

- , , ?

, add_done_callback()? , - , , ?

+6
2

, ( ) , , . , - , . , , , , .

concurrency , , , , . , , .

, , , , ? , , . , , , , . , , , . , .

, , . asyncio concurrent.futures, .

add_done_callbacks , . , . , , . , . .

+4

concurrent.futures.Future , Executor.

asyncio.Future , coroutines, , / . "" , , : , - /.

( threading/asyncio, Lock, Event, Semaphore ..) , concurrency / .

, , , : asyncio , threading concurrent.futures. , concurrent.futures.Future asyncio API.

" "? , , , : asyncio threading , , asyncio/non-asyncio (. async/await).

, , , , : concurrency ( ).

+3

Source: https://habr.com/ru/post/1017122/


All Articles