Skip to content

Instantly share code, notes, and snippets.

@kristjan-eljand
Created March 23, 2021 11:37
Show Gist options
  • Save kristjan-eljand/481b74d35ea85729961488e03895c2da to your computer and use it in GitHub Desktop.
Save kristjan-eljand/481b74d35ea85729961488e03895c2da to your computer and use it in GitHub Desktop.
Counter per second with ray
import ray
# Start ray cluster with 3 cpu's
ray.init(num_cpus=3)
# @ray.remote decorator enables to use this
# function in distributed setting
@ray.remote
def count_per_second(x):
start = time.time()
counter = 0
while time.time() - start < 1:
counter += 1
return counter
start = time.time()
# Run the function 9 times using multiple cores and gather the results
results = ray.get([count_per_second.remote(x) for x in range(9)])
print("duration =", time.time() - start)
print("results =", results)
# Output:
# duration = 3.0135035514831543
# results = [711212, 702964, 708731, 667317, 665449, 667194, 643504, 641866, 647487]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment