"src/vscode:/vscode.git/clone" did not exist on "a0390dde93aed5e81974ba6eab29bef393798836"
utils.py 1.32 KB
Newer Older
Jinjing Zhou's avatar
Jinjing Zhou committed
1
2

import socket
3
import os
Jinjing Zhou's avatar
Jinjing Zhou committed
4
5


6
7
8
def generate_ip_config(file_name, num_machines, num_servers):
    """Get local IP and available ports, writes to file."""
    # get available IP in localhost
Jinjing Zhou's avatar
Jinjing Zhou committed
9
10
11
12
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # doesn't even have to be reachable
        sock.connect(('10.255.255.255', 1))
13
        ip = sock.getsockname()[0]
Jinjing Zhou's avatar
Jinjing Zhou committed
14
    except ValueError:
15
        ip = '127.0.0.1'
Jinjing Zhou's avatar
Jinjing Zhou committed
16
17
    finally:
        sock.close()
18
19
20

    # scan available PORT
    ports = []
Jinjing Zhou's avatar
Jinjing Zhou committed
21
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
22
23
24
25
26
27
28
29
    for port in range(10000, 65535):
        try:
            sock.connect((ip, port))
            ports = []
        except:
            ports.append(port)
            if len(ports) == num_machines * num_servers:
                break
Jinjing Zhou's avatar
Jinjing Zhou committed
30
    sock.close()
31
32
33
34
35
36
    if len(ports) < num_machines * num_servers:
        raise RuntimeError(
            "Failed to get available IP/PORT with required numbers.")
    with open(file_name, 'w') as f:
        for i in range(num_machines):
            f.write('{} {}\n'.format(ip, ports[i*num_servers]))  
37
38
39
40
41
42
43


def reset_envs():
    """Reset common environment variable which are set in tests. """
    for key in ['DGL_ROLE', 'DGL_NUM_SAMPLER', 'DGL_NUM_SERVER', 'DGL_DIST_MODE', 'DGL_NUM_CLIENT']:
        if key in os.environ:
            os.environ.pop(key)