ray_core.py 796 B

1234567891011121314151617181920212223242526272829303132333435
  1. import time
  2. import ray
  3. # By adding the `@ray.remote` decorator, a regular Python function
  4. # becomes a Ray remote function.
  5. @ray.remote
  6. def my_function():
  7. return 1
  8. # To invoke this remote function, use the `remote` method.
  9. # This will immediately return an object ref (a future) and then create
  10. # a task that will be executed on a worker process.
  11. obj_ref = my_function.remote()
  12. # The result can be retrieved with ``ray.get``.
  13. assert ray.get(obj_ref) == 1
  14. @ray.remote
  15. def slow_function():
  16. time.sleep(10)
  17. return 1
  18. # Invocations of Ray remote functions happen in parallel.
  19. # All computation is performed in the background, driven by Ray's internal event loop.
  20. results = []
  21. for _ in range(4):
  22. # this doesn't block
  23. results.append(slow_function.remote())
  24. ray.get(results)