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