Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
68c01ac2
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
68c01ac2
编写于
8月 19, 2022
作者:
K
kuizhiqing
提交者:
GitHub
8月 19, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[LAUNCH] rewrite get free port strategy (#45247)
* rewrite get free port strategy * hide the old one
上级
194d16c1
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
47 addition
and
11 deletion
+47
-11
python/paddle/distributed/launch/context/node.py
python/paddle/distributed/launch/context/node.py
+47
-11
未找到文件。
python/paddle/distributed/launch/context/node.py
浏览文件 @
68c01ac2
...
@@ -14,6 +14,8 @@
...
@@ -14,6 +14,8 @@
from
.device
import
Device
from
.device
import
Device
import
os
import
random
import
socket
import
socket
import
struct
import
struct
from
contextlib
import
closing
from
contextlib
import
closing
...
@@ -28,6 +30,12 @@ class Node(object):
...
@@ -28,6 +30,12 @@ class Node(object):
self
.
free_ports
=
[]
self
.
free_ports
=
[]
self
.
_allocated_ports
=
[]
self
.
_allocated_ports
=
[]
port_range
=
os
.
getenv
(
'PORT_RANGE'
,
'35100:64000'
)
port_range
=
port_range
.
split
(
':'
)
self
.
_port_start
=
int
(
port_range
[
0
])
self
.
_port_end
=
int
(
port_range
[
1
])
self
.
_port_cur
=
random
.
randint
(
self
.
_port_start
,
self
.
_port_end
)
def
get_host_ip
(
self
):
def
get_host_ip
(
self
):
try
:
try
:
self
.
hostname
=
socket
.
gethostname
()
self
.
hostname
=
socket
.
gethostname
()
...
@@ -44,21 +52,44 @@ class Node(object):
...
@@ -44,21 +52,44 @@ class Node(object):
def
get_ports_occupied
(
self
):
def
get_ports_occupied
(
self
):
return
self
.
free_ports
return
self
.
free_ports
def
_get_free_port
(
self
,
port
=
0
):
with
closing
(
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
))
as
s
:
s
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_LINGER
,
struct
.
pack
(
'ii'
,
1
,
0
))
try
:
s
.
bind
((
''
,
port
))
return
s
.
getsockname
()[
1
]
except
:
return
-
1
def
_update_port_cur
(
self
):
self
.
_port_cur
+=
1
if
self
.
_port_cur
>
self
.
_port_end
:
self
.
_port_cur
=
self
.
_port_start
def
get_free_port
(
self
):
def
get_free_port
(
self
):
for
_
in
range
(
100
):
ret
=
self
.
_get_free_port
(
self
.
_port_cur
)
if
ret
>
0
:
self
.
_update_port_cur
()
return
ret
else
:
self
.
_update_port_cur
()
return
self
.
_port_cur
'''
def get_free_port2(self):
# for loop to avoid port conflict
# for loop to avoid port conflict
for _ in range(100):
for _ in range(100):
with
closing
(
socket
.
socket
(
socket
.
AF_INET
,
port = self._get_free_port()
socket
.
SOCK_STREAM
))
as
s
:
if port in self._allocated_ports:
s
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_LINGER
,
continue
struct
.
pack
(
'ii'
,
1
,
0
))
else:
s
.
bind
((
''
,
0
))
self._allocated_ports.append(port)
port
=
s
.
getsockname
()[
1
]
return port
if
port
in
self
.
_allocated_ports
:
continue
else
:
self
.
_allocated_ports
.
append
(
port
)
return
port
return port
return port
'''
@
classmethod
@
classmethod
def
is_server_ready
(
self
,
ip
,
port
):
def
is_server_ready
(
self
,
ip
,
port
):
...
@@ -72,3 +103,8 @@ class Node(object):
...
@@ -72,3 +103,8 @@ class Node(object):
return
True
return
True
else
:
else
:
return
False
return
False
if
__name__
==
'__main__'
:
n
=
Node
()
print
(
n
.
get_free_ports
(
10
))
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录