diff --git a/futures.py b/futures.py new file mode 100644 index 000000000..26e8247c6 --- /dev/null +++ b/futures.py @@ -0,0 +1,14 @@ +import concurrent.futures +import time +import random +import swarms.utils.futures + + +def f(x): + time.sleep(random.random()) + return x + + +with concurrent.futures.ThreadPoolExecutor() as executor: + fs_dict = {str(i): executor.submit(f, i) for i in range(10)} + print(swarms.utils.futures.execute_futures_dict(fs_dict)) diff --git a/swarms/utils/futures.py b/swarms/utils/futures.py index 744b44e0c..bc2d47ef7 100644 --- a/swarms/utils/futures.py +++ b/swarms/utils/futures.py @@ -1,12 +1,38 @@ from concurrent import futures -from typing import TypeVar +from concurrent.futures import Future +from typing import TypeVar, Dict T = TypeVar("T") def execute_futures_dict( - fs_dict: dict[str, futures.Future[T]] -) -> dict[str, T]: + fs_dict: Dict[str, Future[T]] +) -> Dict[str, T]: + """Execute a dictionary of futures and return the results. + + Args: + fs_dict (dict[str, futures.Future[T]]): _description_ + + Returns: + dict[str, T]: _description_ + + Example: + >>> import concurrent.futures + >>> import time + >>> import random + >>> import swarms.utils.futures + >>> def f(x): + ... time.sleep(random.random()) + ... return x + >>> with concurrent.futures.ThreadPoolExecutor() as executor: + ... fs_dict = { + ... str(i): executor.submit(f, i) + ... for i in range(10) + ... } + ... print(swarms.utils.futures.execute_futures_dict(fs_dict)) + {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} + + """ futures.wait( fs_dict.values(), timeout=None,