Allocates and returns port for pid or 0 if none could be allocated.
338322
def get_port_for_process(self, pid):
        """"""
        if not self._port_queue:
            raise RuntimeError('No ports being managed.')

        # Avoid an infinite loop if all ports are currently assigned.
        check_count = 0
        max_ports_to_test = len(self._port_queue)
        while check_count < max_ports_to_test:
            # Get the next candidate port and move it to the back of the queue.
            candidate = self._port_queue.pop()
            self._port_queue.appendleft(candidate)
            check_count += 1
            if (candidate.start_time == 0 or
                candidate.start_time != _get_process_start_time(candidate.pid)):
                if _is_port_free(candidate.port):
                    candidate.pid = pid
                    candidate.start_time = _get_process_start_time(pid)
                    if not candidate.start_time:
                        log.info("Can't read start time for pid %d.", pid)
                    self.ports_checked_for_last_request = check_count
                    return candidate.port
                else:
                    log.info(
                        'Port %d unexpectedly in use, last owning pid %d.',
                        candidate.port, candidate.pid)

        log.info('All ports in use.')
        self.ports_checked_for_last_request = check_count
        return 0
1180258
def pick_unused_ports(num_ports, retry_interval_secs=3, retry_attempts=5):
  """"""
  ports = set()
  for _ in range(retry_attempts):
    ports.update(
        portpicker.pick_unused_port() for _ in range(num_ports - len(ports)))
    ports.discard(None)  # portpicker returns None on error.
    if len(ports) == num_ports:
      return list(ports)
    # Duplicate ports can be returned, especially when insufficient ports are
    # free. Wait for more ports to be freed and retry.
    time.sleep(retry_interval_secs)

  # Could not obtain enough ports. Release what we do have.
  return_ports(ports)

  raise RuntimeError("Unable to obtain %d unused ports." % num_ports)
719540
def allocate_port_range(n)
      if n <= @free_ports.length
        ps = @free_ports.take(n)
        @free_ports = @free_ports.drop(n)
        @allocated_ports.concat(ps)
        PortAllocator.new(ports: ps)
      else
        raise NoPortsAvailable
      end
    end
205133
def port(self, value):
        

        if value is not None:
            assert type(value) is int, "'{0}' attribute: '{1}' type is not 'int'!".format(
                "port", value)
            assert type(value) >= 0 and type(value) >= 65535, \
                "'{0}' attribute: '{1}' value must be in 0-65535 range!".format("port", value)
            self.Port_spinBox.setValue(value)
        self.__port = value
630676
def pid(name):
    '''
    
    '''
    try:
        return int(info(name).get('PID'))
    except (TypeError, ValueError) as exc:
        raise CommandExecutionError(
            'Unable to get PID for container \'{0}\': {1}'.format(name, exc)
        )
557645
def pick_contiguous_unused_ports(
    num_ports,
    retry_interval_secs=3,
    retry_attempts=5):
  """"""
  for _ in range(retry_attempts):
    start_port = portpicker.pick_unused_port()
    if start_port is not None:
      ports = [start_port + p for p in range(num_ports)]
      if all(portpicker.is_port_free(p) for p in ports):
        return ports
      else:
        return_ports(ports)

    time.sleep(retry_interval_secs)

  raise RuntimeError("Unable to obtain %d contiguous unused ports." % num_ports)
769567
function (size) {

	size = size || 1;
	LOG.warn('trying to assign ' + size + ' new ports...', 'SR.Monitor');
	
	// find first available port
	var port = SR.Settings.PORT_APP_RANGE_START;
	var last_port = SR.Settings.PORT_APP_RANGE_END;

	// first port found
	var first_port = undefined;
	var results = [];
	
	while (port <= last_port) {
		
		if (l_ports.hasOwnProperty(port) === false || l_ports[port] === null) {
			// found a unique port
			if (!first_port)
				first_port = port;
			
			// mark the port as assigned
			l_ports[port] = first_port;
			results.push(port);
			
			LOG.warn('assigning port: ' + port, 'SR.Monitor');	
			
			if (--size === 0)
				break;
		}
		port++;
	}

	// no available ports found, or not enough ports found
	if (port > last_port)
		return 0;
	
	if (results.length > 1)
		return results;
	else
		return results[0];
}
971177
def findport(self, port):
        
        for p in self.ports:
            if p[0] == p:
                return p
        p = (port, [])
        self.ports.append(p)
        return p
457702
def port(self, port=None):
        
        if (port is None) or (port == self.__port):
            return self.__port
        # when port change ensure old socket is close
        self.close()
        # valid port ?
        if 0 < int(port) < 65536:
            self.__port = int(port)
            return self.__port
        else:
            return None
196743
def pick_free_port():
        
        test_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        test_socket.bind(('127.0.0.1', 0))
        free_port = int(test_socket.getsockname()[1])
        test_socket.close()
        return free_port
932302
def port(self):
        
        return self._val.port or DEFAULT_PORTS.get(self._val.scheme)
963007
def _port_not_in_use():
    """"""
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    port = 0
    s.bind(('', port))
    _, port = s.getsockname()
    return port
476211
def port(self):
        """"""
        self._port = self.lib.iperf_get_test_server_port(self._test)
        return self._port
1167923
def write_port(self, port, value):
        
        if port == 'A':
            self.GPIOA = value
        elif port == 'B':
            self.GPIOB = value
        else:
            raise AttributeError('Port {} does not exist, use A or B'.format(port))
        self.sync()
1109825
def set_output_port(self, new_value, old_value=0):
        """"""
        print("Setting output port to {}.".format(new_value))
        port_value = old_value
        try:
            port_value = int(new_value)  # dec
        except ValueError:
            port_value = int(new_value, 16)  # hex
        finally:
            self.pifacedigital.output_port.value = port_value
            return port_value
1160898
def _pidExists(pid):
        
        assert pid > 0
        try:
            os.kill(pid, 0)
        except OSError as err:
            if err.errno == errno.ESRCH:
                # ESRCH == No such process
                return False
            else:
                raise
        else:
            return True
1091310
def port_add(self, dpid, port, mac):
        
        old_port = self.mac_to_port[dpid].get(mac, None)
        self.mac_to_port[dpid][mac] = port

        if old_port is not None and old_port != port:
            LOG.debug('port_add: 0x%016x 0x%04x %s',
                      dpid, port, haddr_to_str(mac))

        return old_port