Search⌘ K
AI Features

Deadlocks

Explore how to use Python's concurrent.futures module to run asynchronous tasks without causing deadlocks. Understand common pitfalls with waiting on nested futures and learn best practices with ThreadPoolExecutor and ProcessPoolExecutor to manage concurrent operations efficiently.

Pitfalls of the concurrent.futures module

One of the pitfalls of the concurrent.futures module is that you can accidentally create deadlocks when the caller to associate with a Future is also waiting on the results of another future.

This sounds kind of confusing, so let’s look at an example:

Python 3.5
from concurrent.futures import ThreadPoolExecutor
def wait_forever():
"""
This function will wait forever if there's only one
thread assigned to the pool
"""
my_future = executor.submit(zip, [1, 2, 3], [4, 5, 6])
result = my_future.result()
print(result)
if __name__ == '__main__':
executor = ThreadPoolExecutor(max_workers=1)
executor.submit(wait_forever)

Here we import the ThreadPoolExecutor class ...