Commit 00459999 authored by André Anjos's avatar André Anjos 💬
Browse files

Get an actual free random port instead of a random port number

parent 16d368b5
Pipeline #19448 failed with stages
in 32 minutes and 6 seconds
......@@ -32,7 +32,6 @@ import os
import requests
import simplejson
import zmq
import random
import logging
logger = logging.getLogger(__name__)
......@@ -254,7 +253,7 @@ class DockerExecutor(RemoteExecutor):
# Creation of the container
# Note: we only support one databases image loaded at the same time
database_port = random.randint(51000, 60000)
database_port = utils.find_free_port()
cmd = [
'databases_provider',
......@@ -293,7 +292,7 @@ class DockerExecutor(RemoteExecutor):
break
databases_container.reset_ports()
database_port = random.randint(51000, 60000)
database_port = utils.find_free_port()
cmd = [x if not x.startswith('0.0.0.0:') else '0.0.0.0:%d' % database_port for x in cmd]
databases_container.command = cmd
......
......@@ -33,7 +33,6 @@ from __future__ import absolute_import
import os
import requests
import zmq
import random
import sys
import subprocess as sp
import tempfile
......@@ -240,7 +239,7 @@ class SubprocessExecutor(RemoteExecutor):
#----- (If necessary) Instantiate the subprocess that provide the databases
databases_process = None
database_port = random.randint(51000, 60000)
database_port = utils.find_free_port()
if len(self.databases) > 0:
......
......@@ -29,6 +29,8 @@
import os
import sys
import tempfile
import socket
import contextlib
import numpy
import simplejson
......@@ -66,6 +68,9 @@ def uniq(seq):
return result
#----------------------------------------------------------
def send_multipart(socket, parts):
"""
Send the parts through the socket after having encoded them if
......@@ -77,3 +82,15 @@ def send_multipart(socket, parts):
parts[index] = item.encode('utf-8')
socket.send_multipart(parts)
#----------------------------------------------------------
def find_free_port():
'''Returns the value of a free random port'''
with contextlib.closing(socket.socket(socket.AF_INET,
socket.SOCK_STREAM)) as s:
s.bind(('', 0))
return s.getsockname()[1]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment