Skip to content
Snippets Groups Projects
Commit 00459999 authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

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

parent 16d368b5
No related branches found
No related tags found
2 merge requests!32Merge development branch 1.6.x,!19New Conda-based CI/CD Pipelines
Pipeline #
...@@ -32,7 +32,6 @@ import os ...@@ -32,7 +32,6 @@ import os
import requests import requests
import simplejson import simplejson
import zmq import zmq
import random
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -254,7 +253,7 @@ class DockerExecutor(RemoteExecutor): ...@@ -254,7 +253,7 @@ class DockerExecutor(RemoteExecutor):
# Creation of the container # Creation of the container
# Note: we only support one databases image loaded at the same time # 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 = [ cmd = [
'databases_provider', 'databases_provider',
...@@ -293,7 +292,7 @@ class DockerExecutor(RemoteExecutor): ...@@ -293,7 +292,7 @@ class DockerExecutor(RemoteExecutor):
break break
databases_container.reset_ports() 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] 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 databases_container.command = cmd
......
...@@ -33,7 +33,6 @@ from __future__ import absolute_import ...@@ -33,7 +33,6 @@ from __future__ import absolute_import
import os import os
import requests import requests
import zmq import zmq
import random
import sys import sys
import subprocess as sp import subprocess as sp
import tempfile import tempfile
...@@ -240,7 +239,7 @@ class SubprocessExecutor(RemoteExecutor): ...@@ -240,7 +239,7 @@ class SubprocessExecutor(RemoteExecutor):
#----- (If necessary) Instantiate the subprocess that provide the databases #----- (If necessary) Instantiate the subprocess that provide the databases
databases_process = None databases_process = None
database_port = random.randint(51000, 60000) database_port = utils.find_free_port()
if len(self.databases) > 0: if len(self.databases) > 0:
......
...@@ -29,6 +29,8 @@ ...@@ -29,6 +29,8 @@
import os import os
import sys import sys
import tempfile import tempfile
import socket
import contextlib
import numpy import numpy
import simplejson import simplejson
...@@ -66,6 +68,9 @@ def uniq(seq): ...@@ -66,6 +68,9 @@ def uniq(seq):
return result return result
#----------------------------------------------------------
def send_multipart(socket, parts): def send_multipart(socket, parts):
""" """
Send the parts through the socket after having encoded them if Send the parts through the socket after having encoded them if
...@@ -77,3 +82,15 @@ def send_multipart(socket, parts): ...@@ -77,3 +82,15 @@ def send_multipart(socket, parts):
parts[index] = item.encode('utf-8') parts[index] = item.encode('utf-8')
socket.send_multipart(parts) 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]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment