Nesting concurrent.futures.ThreadPoolExecutor

I have a program in which I am currently using concurrent.futures.ThreadPoolExecutor to run multiple tasks at the same time. These tasks are usually associated with I / O, including access to local databases and remote REST APIs. However, these tasks can be divided into subtasks, which will also benefit concurrency.

I hope it is safe to use concurrent.futures.ThreadPoolExecutor in my tasks. I have encoded a toy example that seems to work:

import concurrent.futures


def inner(i, j):
    return i, j, i**j


def outer(i):
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        futures = {executor.submit(inner, i, j): j for j in range(5)}
        results = []
        for future in concurrent.futures.as_completed(futures):
            results.append(future.result())
    return results


def main():
    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
        futures = {executor.submit(outer, i): i for i in range(10)}
        results = []
        for future in concurrent.futures.as_completed(futures):
            results.extend(future.result())
    print(results)


if __name__ == "__main__":
    main()

, , , , . , , , concurrent.futures concurrency.

+4

Source: https://habr.com/ru/post/1681030/


All Articles