|
2 | 2 | import logging |
3 | 3 | import traceback |
4 | 4 | import time |
| 5 | +import socket |
| 6 | +from contextlib import closing |
5 | 7 |
|
6 | 8 | from rafiki.db import Database |
7 | 9 | from rafiki.constants import ServiceStatus, UserType, ServiceType, BudgetType |
8 | | -from rafiki.config import MIN_SERVICE_PORT, MAX_SERVICE_PORT, \ |
9 | | - TRAIN_WORKER_REPLICAS_PER_SUB_TRAIN_JOB, INFERENCE_WORKER_REPLICAS_PER_TRIAL, \ |
| 10 | +from rafiki.config import TRAIN_WORKER_REPLICAS_PER_SUB_TRAIN_JOB, INFERENCE_WORKER_REPLICAS_PER_TRIAL, \ |
10 | 11 | INFERENCE_MAX_BEST_TRIALS, SERVICE_STATUS_WAIT |
11 | 12 | from rafiki.container import DockerSwarmContainerManager, ServiceRequirement, InvalidServiceRequest |
12 | 13 | from rafiki.model import parse_model_install_command |
@@ -347,18 +348,12 @@ def _create_service(self, service_type, docker_image, |
347 | 348 |
|
348 | 349 | return service |
349 | 350 |
|
350 | | - # Compute next available external port |
351 | 351 | def _get_available_ext_port(self): |
352 | | - services = self._db.get_services(status=ServiceStatus.RUNNING) |
353 | | - used_ports = [int(x.ext_port) for x in services if x.ext_port is not None] |
354 | | - port = MIN_SERVICE_PORT |
355 | | - while port <= MAX_SERVICE_PORT: |
356 | | - if port not in used_ports: |
357 | | - return port |
358 | | - |
359 | | - port += 1 |
360 | | - |
361 | | - return port |
| 352 | + # Credits to https://stackoverflow.com/questions/1365265/on-localhost-how-do-i-pick-a-free-port-number |
| 353 | + with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: |
| 354 | + s.bind(('', 0)) |
| 355 | + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 356 | + return s.getsockname()[1] |
362 | 357 |
|
363 | 358 | def _get_best_trials_for_inference(self, inference_job): |
364 | 359 | best_trials = self._db.get_best_trials_of_train_job(inference_job.train_job_id) |
|
0 commit comments