Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `concurrent.futures.Future` as promise

In the Python docs I see:

concurrent.futures.Future... ...should not be created directly except for testing.

And I want to use it as a promise in my code and I'm very surprised that it is not recommended to use it like this.

My use case:
I have a single thread that reads data packets coming from socket, and I have many callbacks that are called depending on some information contained in packets. Packets are responses to consumers requests, and all consumers use single connection. Each consumer receives a promise and adds some handlers to it, that are invoked when response arrives.

So I cant use Executor subclass here, because I have only one thread, but I need to create many Futures (promises).

Promise is pretty widespread programming technique and, I thought that Future is Python's promise implementation. But if it is not recommended to use it like promise, what pythonistas are commonly use for this purpose?

Note

I use Python 2.7 backport of concurrent.futures to 2.7

like image 619
Gill Bates Avatar asked Nov 10 '14 08:11

Gill Bates


People also ask

Is concurrent futures thread-safe?

Yes, it's thread-safe.

Are futures and promises the same?

Futures and promises are pretty similar concepts, the difference is that a future is a read-only container for a result that does not yet exist, while a promise can be written (normally only once).

How does concurrent futures ThreadPoolExecutor work?

ThreadPoolExecutor Methods : submit(fn, *args, **kwargs): It runs a callable or a method and returns a Future object representing the execution state of the method. map(fn, *iterables, timeout = None, chunksize = 1) : It maps the method and iterables together immediately and will raise an exception concurrent. futures.

What is the use of concurrent futures module?

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. Both implement the same interface, which is defined by the abstract Executor class. 17.4.1.

What is the use of executor in 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. Both implement the same interface, which is defined by the abstract Executor class. 17.4.1. Executor Objects ¶

What is concurrent futures wait in Oracle?

concurrent.futures. wait (fs, timeout=None, return_when=ALL_COMPLETED) ¶ Wait for the Future instances (possibly created by different Executor instances) given by fs to complete. Returns a named 2-tuple of sets. The first set, named done, contains the futures that completed (finished or cancelled futures) before the wait completed.

What does as_completed () return in concurrent futures?

concurrent.futures. as_completed (fs, timeout=None) ¶ Returns an iterator over the Future instances (possibly created by different Executor instances) given by fs that yields futures as they complete (finished or cancelled futures). Any futures given by fs that are duplicated will be returned once.


1 Answers

It's perfectly fine to use Future in order to wrap non-promise APIs into promises.

The reason it generally should not be created is because most times people create futures directly it's because they're doing the deferred anti pattern and wrapping an executor created future in another future.

It's worth mentioning that this future implementation is very weak, it's akin to Java's old futures, the cool stuff promises give you like chaining is simply missing. It's worth mentioning that languages like JavaScript got their promises from Python's Twisted, which has a better implementation, even if it's intertwined with other things.

like image 183
Benjamin Gruenbaum Avatar answered Oct 26 '22 02:10

Benjamin Gruenbaum