What is the difference between concurrent.futures and asyncio.futures?
Question:
To clarify the reason for this question:
-
It is confusing to use two modules with the same name. What do they represent that makes them distinct?
-
What task(s) can one solve that the other can’t and vice-versa?
Answers:
From the docs:
[asyncio
provides a] Future class that mimics the one in the concurrent.futures module, but adapted for use with the event loop;
The asyncio
documentation covers the differences:
class asyncio.Future(*, loop=None)
This class is almost compatible with concurrent.futures.Future
.
Differences:
result()
and exception()
do not take a timeout argument and raise an exception when the future isn’t done yet.
- Callbacks registered with
add_done_callback()
are always called via the event loop’s call_soon_threadsafe()
.
- This class is not compatible with the
wait()
and as_completed()
functions in the concurrent.futures
package.
This class is not thread safe.
Basically, if you’re using ThreadPoolExecutor
or ProcessPoolExecutor
, or want to use a Future
directly for thread-based or process-based concurrency, use concurrent.futures.Future
. If you’re using asyncio
, use asyncio.Future
.
To clarify the reason for this question:
-
It is confusing to use two modules with the same name. What do they represent that makes them distinct?
-
What task(s) can one solve that the other can’t and vice-versa?
From the docs:
[
asyncio
provides a] Future class that mimics the one in the concurrent.futures module, but adapted for use with the event loop;
The asyncio
documentation covers the differences:
class
asyncio.Future(*, loop=None)
This class is almost compatible with
concurrent.futures.Future
.Differences:
result()
andexception()
do not take a timeout argument and raise an exception when the future isn’t done yet.- Callbacks registered with
add_done_callback()
are always called via the event loop’scall_soon_threadsafe()
.- This class is not compatible with the
wait()
andas_completed()
functions in theconcurrent.futures
package.This class is not thread safe.
Basically, if you’re using ThreadPoolExecutor
or ProcessPoolExecutor
, or want to use a Future
directly for thread-based or process-based concurrency, use concurrent.futures.Future
. If you’re using asyncio
, use asyncio.Future
.