Skip to content

Instantly share code, notes, and snippets.

@Seanny123
Created December 15, 2021 16:59
Show Gist options
  • Save Seanny123/d869c0e067696d247adbcaa5c638bdc5 to your computer and use it in GitHub Desktop.
Save Seanny123/d869c0e067696d247adbcaa5c638bdc5 to your computer and use it in GitHub Desktop.
Example of using concurrent.futures to manage Ray execution
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