How to use python ray for independent computers (each have its username and password) via internet(distributed computation with ip address)?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Distributed computing allows multiple computers to work together on a common problem, breaking it down into smaller parts that are solved simultaneously, hence reducing the total computation time. Python's Ray library is an excellent tool for achieving distributed computing across machines. Here we will explore how to set up and use Ray for distributed computation across independent computers connected over the internet.
Understanding Ray
Ray is an open-source framework that simplifies the scaling of applications from a single machine to a large cluster, without modifying the code. It is designed to scale Python applications, including machine learning models and algorithms, across multiple nodes.
Key Components of Ray:
- Ray Nodes: A Ray cluster consists of at least one head node and multiple worker nodes. The head node manages the cluster and handles task scheduling and distribution among the worker nodes.
- Ray Tasks and Actors: Ray abstracts its computations through tasks (stateless functions) and actors (stateful objects), which can be executed in parallel across cluster nodes.
Preliminary Steps
Before setting up Ray for distributed computation, make sure:
- All machines are accessible via an IP address over the internet.
- Python and Ray are installed on all machines (
pip install ray). - Appropriate network settings (e.g., firewalls, port forwarding) are configured to allow communication.
Setting Up Ray Over the Internet
- Establish Secure Connections: Because each node will likely be protected by a username and password and might require secure connections, setting up SSH keys is recommended to facilitate secure, password-less logins.
- Configuring SSH for Ray:
- On each worker node, generate an SSH key using
ssh-keygen. - Copy the public key to the head node’s authorized keys using
ssh-copy-id.
- Initialize the Ray Cluster:
- Start Ray on the head node specifying the port and include any necessary configurations:
- On each worker node, connect them to the head node:
Running a Distributed Task
Here is an example in Python that demonstrates setting up and executing a simple distributed computation:
Key Points Table
| Key Aspect | Detail |
| Installation | pip install ray on all nodes |
| Secure Connection | Use SSH with keys for secure communication |
| Head Node Init | ray start --head --port=6379 |
| Worker Node Init | ray start --address=<head_ip>:6379 |
| Code Execution | Use @ray.remote decorator for functions |
Additional Considerations
- Security: Ensure that all communication between the nodes is secured, not just during the initial SSH but also the subsequent data transfers.
- Network Latency: Nodes over the internet could have significant latency; optimize task granularity to mitigate latency overhead.
- Error Handling: Implement robust error handling, especially network-related exceptions.
Conclusion
Using Ray to manage distributed computations across multiple computers via the internet involves several setup steps but can drastically increase computational power and efficiency. Securely connecting nodes and efficiently structuring tasks are key to harnessing the full potential of distributed computing with Ray.
Related reading
- How to view the Folder and Files in GAC?
- How to write a scalable TCP/IP based server
- How to write more than 25 items/rows into Table for DynamoDB?
- How would you implement an LRU cache in Java?
- How to use ReadDirectoryChangesW method with completion routine?
- How to use RestSharp with async/await
- How to use Python to execute a cURL command?
- How to use Python type hints with Django QuerySet?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.