def SetupPrometheusEndpointOnPortRange(port_range, addr=''):
    """Like SetupPrometheusEndpointOnPort, but tries several ports.

    This is useful when you're running Django as a WSGI application
    with multiple processes and you want Prometheus to discover all
    workers. Each worker will grab a port and you can use Prometheus
    to aggregate across workers.

    port_range may be any iterable object that contains a list of
    ports. Typically this would be an xrange of contiguous ports.

    As soon as one port is found that can serve, use this one and stop
    trying.

    The same caveats regarding autoreload apply. Do not use this when
    Django's autoreloader is active.

    """
    assert os.environ.get('RUN_MAIN') != 'true', (
        'The thread-based exporter can\'t be safely used when django\'s '
        'autoreloader is active. Use the URL exporter, or start django '
        'with --noreload. See documentation/exports.md.')
    for port in port_range:
        try:
            httpd = HTTPServer((addr, port), prometheus_client.MetricsHandler)
        except (OSError, socket.error):
            # Python 2 raises socket.error, in Python 3 socket.error is an
            # alias for OSError
            continue  # Try next port
        thread = PrometheusEndpointServer(httpd)
        thread.daemon = True
        thread.start()
        logger.info('Exporting Prometheus /metrics/ on port %s' % port)
        return
Like SetupPrometheusEndpointOnPort, but tries several ports.

    This is useful when you're running Django as a WSGI application
    with multiple processes and you want Prometheus to discover all
    workers. Each worker will grab a port and you can use Prometheus
    to aggregate across workers.

    port_range may be any iterable object that contains a list of
    ports. Typically this would be an xrange of contiguous ports.

    As soon as one port is found that can serve, use this one and stop
    trying.

    The same caveats regarding autoreload apply. Do not use this when
    Django's autoreloader is active.