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?
Future Object. class asyncio. Future (*, loop=None) A Future represents an eventual result of an asynchronous operation.
The concurrent. futures module provides a high-level interface for asynchronously executing callables. The asynchronous execution can be performed with threads, using ThreadPoolExecutor , or separate processes, using ProcessPoolExecutor .
Asyncio stands for asynchronous input output and refers to a programming paradigm which achieves high concurrency using a single thread or event loop.
One of the cool advantages of asyncio is that it scales far better than threading . Each task takes far fewer resources and less time to create than a thread, so creating and running more of them works well. This example just creates a separate task for each site to download, which works out quite well.
class asyncio. Future (*, loop=None) ¶ A Future represents an eventual result of an asynchronous operation. Not thread-safe. Future is an awaitable object.
This class is almost compatible with concurrent.futures.Future. 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 ().
asyncio.Future isn't thread-safe at all - it's only designed to be used in a single-threaded, asyncio -based application. If you want to call a method on asyncio.Future from a thread outside of the event loop thread, you'd need to use loop.call_soon_threadsafe.
Coroutines can await on Future objects until they either have a result or an exception set, or until they are cancelled. Typically Futures are used to enable low-level callback-based code (e.g. in protocols implemented using asyncio transports ) to interoperate with high-level async/await code.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With