Skip to content

ding.utils.system_helper

ding.utils.system_helper

PropagatingThread

Bases: Thread

Overview

Subclass of Thread that propagates execution exception in the thread to the caller

Interfaces: run, join Examples: >>> def func(): >>> raise Exception() >>> t = PropagatingThread(target=func, args=()) >>> t.start() >>> t.join()

run()

Overview

Run the thread

join()

Overview

Join the thread

get_ip()

Overview

Get the ip(host) of socket

Returns: - ip(:obj:str): The corresponding ip

get_pid()

Overview

os.getpid

get_task_uid()

Overview

Get the slurm job_id, pid and uid

find_free_port(host)

Overview

Look up the free port list and return one

Arguments: - host (:obj:str): The host

Full Source Code

../ding/utils/system_helper.py

1import os 2import socket 3import time 4import uuid 5from contextlib import closing 6from threading import Thread 7from typing import Any 8 9 10def get_ip() -> str: 11 """ 12 Overview: 13 Get the ``ip(host)`` of socket 14 Returns: 15 - ip(:obj:`str`): The corresponding ip 16 """ 17 # beware: return 127.0.0.1 on some slurm nodes 18 myname = socket.getfqdn(socket.gethostname()) 19 myaddr = socket.gethostbyname(myname) 20 21 return myaddr 22 23 24def get_pid() -> int: 25 """ 26 Overview: 27 ``os.getpid`` 28 """ 29 return os.getpid() 30 31 32def get_task_uid() -> str: 33 """ 34 Overview: 35 Get the slurm ``job_id``, ``pid`` and ``uid`` 36 """ 37 return '{}_{}'.format(str(uuid.uuid4()), str(time.time())[-6:]) 38 39 40class PropagatingThread(Thread): 41 """ 42 Overview: 43 Subclass of Thread that propagates execution exception in the thread to the caller 44 Interfaces: 45 ``run``, ``join`` 46 Examples: 47 >>> def func(): 48 >>> raise Exception() 49 >>> t = PropagatingThread(target=func, args=()) 50 >>> t.start() 51 >>> t.join() 52 """ 53 54 def run(self) -> None: 55 """ 56 Overview: 57 Run the thread 58 """ 59 60 self.exc = None 61 try: 62 self.ret = self._target(*self._args, **self._kwargs) 63 except Exception as e: 64 self.exc = e 65 66 def join(self) -> Any: 67 """ 68 Overview: 69 Join the thread 70 """ 71 72 super(PropagatingThread, self).join() 73 if self.exc: 74 raise RuntimeError('Exception in thread({})'.format(id(self))) from self.exc 75 return self.ret 76 77 78def find_free_port(host: str) -> int: 79 """ 80 Overview: 81 Look up the free port list and return one 82 Arguments: 83 - host (:obj:`str`): The host 84 """ 85 with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: 86 s.bind(('', 0)) 87 return s.getsockname()[1]