Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between concurrent.futures and asyncio.futures?

To clarify the reason for this question:

  1. It is confusing to use two modules with the same name. What do they represent that makes them distinct?

  2. What task(s) can one solve that the other can't and vice-versa?

like image 318
sargas Avatar asked Apr 27 '15 18:04

sargas


People also ask

What is an Asyncio future?

Future Object. class asyncio. Future (*, loop=None) A Future represents an eventual result of an asynchronous operation.

What are concurrent futures?

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 .

Is Asyncio a concurrency?

Asyncio stands for asynchronous input output and refers to a programming paradigm which achieves high concurrency using a single thread or event loop.

Is Asyncio better than threading?

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.

What is future in asyncio?

class asyncio. Future (*, loop=None) ¶ A Future represents an eventual result of an asynchronous operation. Not thread-safe. Future is an awaitable object.

Is this class compatible with concurrent futures?

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 ().

Is asyncio future thread safe?

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.

Can coroutines await on futures?

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.


1 Answers

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.

like image 155
dano Avatar answered Sep 16 '22 20:09

dano