Skip to content

Instantly share code, notes, and snippets.

@dwerbam
Created October 9, 2021 23:29
Show Gist options
  • Save dwerbam/9df3b31db060d0883d4a3338c7d79c55 to your computer and use it in GitHub Desktop.
Save dwerbam/9df3b31db060d0883d4a3338c7d79c55 to your computer and use it in GitHub Desktop.
example of how to use ray.io to execute multiple OS processes in parallel (something like GNU parallel is doing)
# example of how to use ray.io to execute multiple
# OS processes in parallel (even in multiple machines)
# (something like GNU parallel is doing)
pip install -U "ray[default]"
import ray
import os
import time
os.environ["RAY_DISABLE_MEMORY_MONITOR"] = "1"
ray.init(include_dashboard=True)
# %%
@ray.remote
def remote_exec(i):
import subprocess
list_files = subprocess.run(["ls", "-l"], capture_output=True, text=True)
# print("The exit code was: %d" % list_files.returncode)
out = list_files.stdout.split('\n')
return out
futures = [remote_exec.remote(i) for i in range(10)]
print(ray.get(futures))
print("finished")
# %%
ray.shutdown()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment