Futures
Let's start looking at a more asynchronous way of implementing concurrency. The concept of a "future" or a "promise" is a handy abstraction for describing concurrent work. A future is an object that wraps a function call. That function call is run in the background, in a thread or a separate process. The future
object has methods to check whether the computation has completed and to get the results. We can think of it as a computation where the results will arrive in the future, and we can do something else while waiting for them.
See https://hub.packtpub.com/asynchronous-programming-futures-and-promises/ for some additional background.
In Python, the concurrent.futures
module wraps either multiprocessing
or threading
depending on what kind of concurrency we need. A future doesn't completely solve the problem of accidentally altering shared state, but using futures allows us to structure our code such that it can be easier to track...