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

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


7
8
9
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
10
11
12
13
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        # doesn't even have to be reachable
        sock.connect(('10.255.255.255', 1))
14
        ip = sock.getsockname()[0]
Jinjing Zhou's avatar
Jinjing Zhou committed
15
    except ValueError:
16
        ip = '127.0.0.1'
Jinjing Zhou's avatar
Jinjing Zhou committed
17
18
    finally:
        sock.close()
19
20
21

    # scan available PORT
    ports = []
Jinjing Zhou's avatar
Jinjing Zhou committed
22
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
23
24
    start = random.randint(10000, 30000)
    for port in range(start, 65535):
25
26
27
28
29
30
31
        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
32
    sock.close()
33
34
35
36
37
38
    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]))  
39
40
41
42


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