Created
December 15, 2021 16:59
-
-
Save Seanny123/d869c0e067696d247adbcaa5c638bdc5 to your computer and use it in GitHub Desktop.
Example of using concurrent.futures to manage Ray execution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import concurrent.futures | |
import random | |
import time | |
import ray | |
@ray.remote | |
def append_a(in_str): | |
print(in_str) | |
time.sleep(random.uniform(0, 1.5)) | |
return f"{in_str}a" | |
ray.init() | |
with concurrent.futures.ThreadPoolExecutor(max_workers=80) as threader: | |
inputs = ["a", "b", "c", "d", "e", "f", "g"] | |
outputs = [] | |
submitted = threader.map(append_a.remote, inputs) | |
futures = {task.future(): inp for task, inp in zip(submitted, inputs)} | |
for future in concurrent.futures.as_completed(futures): | |
outputs.append((futures[future], future.result())) | |
print(outputs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment