未验证 提交 6473e27d 编写于 作者: W WangXi 提交者: GitHub

fix launch port already in use (#32892)

上级 2d9d8f57
...@@ -12,9 +12,7 @@ ...@@ -12,9 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import functools
import logging import logging
import socket
import time import time
import os import os
import signal import signal
...@@ -27,6 +25,7 @@ from contextlib import closing ...@@ -27,6 +25,7 @@ from contextlib import closing
import socket import socket
import warnings import warnings
import six import six
import struct
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
...@@ -362,6 +361,10 @@ def add_arguments(argname, type, default, help, argparser, **kwargs): ...@@ -362,6 +361,10 @@ def add_arguments(argname, type, default, help, argparser, **kwargs):
def find_free_ports(num): def find_free_ports(num):
def __free_port(): def __free_port():
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
# Note(wangxi): Close the connection with a TCP RST instead
# of a TCP FIN, to avoid time_wait state.
s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
struct.pack('ii', 1, 0))
s.bind(('', 0)) s.bind(('', 0))
return s.getsockname()[1] return s.getsockname()[1]
...@@ -376,7 +379,7 @@ def find_free_ports(num): ...@@ -376,7 +379,7 @@ def find_free_ports(num):
return port_set return port_set
step += 1 step += 1
if step > 100: if step > 400:
print( print(
"can't find avilable port and use the specified static port now!" "can't find avilable port and use the specified static port now!"
) )
......
...@@ -24,6 +24,7 @@ import paddle.fluid as fluid ...@@ -24,6 +24,7 @@ import paddle.fluid as fluid
from argparse import ArgumentParser, REMAINDER from argparse import ArgumentParser, REMAINDER
from paddle.distributed.utils import _print_arguments, get_gpus, get_cluster_from_args from paddle.distributed.utils import _print_arguments, get_gpus, get_cluster_from_args
from paddle.distributed.fleet.launch_utils import find_free_ports
def _parse_args(): def _parse_args():
...@@ -115,6 +116,9 @@ class TestCoverage(unittest.TestCase): ...@@ -115,6 +116,9 @@ class TestCoverage(unittest.TestCase):
args.use_paddlecloud = True args.use_paddlecloud = True
cluster, pod = get_cluster_from_args(args, "0") cluster, pod = get_cluster_from_args(args, "0")
def test_find_free_ports(self):
find_free_ports(2)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册