How to use the portpicker.is_port_free function in portpicker

To help you get started, we’ve selected a few portpicker examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github deepmind / pysc2 / pysc2 / bin / play_vs_agent.py View on Github external
def human():
  """Run a host which expects one player to connect remotely."""
  run_config = run_configs.get()

  map_inst = maps.get(FLAGS.map)

  if not FLAGS.rgb_screen_size or not FLAGS.rgb_minimap_size:
    logging.info("Use --rgb_screen_size and --rgb_minimap_size if you want rgb "
                 "observations.")

  ports = [FLAGS.config_port + p for p in range(5)]  # tcp + 2 * num_players
  if not all(portpicker.is_port_free(p) for p in ports):
    sys.exit("Need 5 free ports after the config port.")

  proc = None
  ssh_proc = None
  tcp_conn = None
  udp_sock = None
  try:
    proc = run_config.start(extra_ports=ports[1:], timeout_seconds=300,
                            host=FLAGS.host, window_loc=(50, 50))

    tcp_port = ports[0]
    settings = {
        "remote": FLAGS.remote,
        "game_version": proc.version.game_version,
        "realtime": FLAGS.realtime,
        "map_name": map_inst.name,
github deepmind / pysc2 / pysc2 / lib / portspicker.py View on Github external
def pick_contiguous_unused_ports(
    num_ports,
    retry_interval_secs=1,
    retry_attempts=5):
  """Reserves and returns a list of `num_ports` contiguous unused ports."""
  if num_ports <= 0:
    raise ValueError("Number of ports, must be >= 1, got: %s" % num_ports)
  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):
        _contiguous_ports.update(ports[1:])
        return ports
      else:
        portpicker.return_port(start_port)

    time.sleep(retry_interval_secs)

  raise RuntimeError("Unable to obtain %d contiguous unused ports." % num_ports)
github qing8137 / StarCraft2-Happy-Kitting / DQN / pysc2 / bin / play_vs_agent.py View on Github external
def host():
  """Run a host which expects one player to connect remotely."""
  run_config = run_configs.get()

  map_inst = maps.get(FLAGS.map)

  if not FLAGS.rgb_screen_size or not FLAGS.rgb_minimap_size:
    logging.info("Use --rgb_screen_size and --rgb_minimap_size if you want rgb "
                 "observations.")

  while True:
    start_port = portpicker.pick_unused_port()
    ports = [start_port + p for p in range(4)]  # 2 * num_players
    if all(portpicker.is_port_free(p) for p in ports):
      break

  host_proc = run_config.start(extra_ports=ports, host=FLAGS.host,
                               timeout_seconds=300, window_loc=(50, 50))
  client_proc = run_config.start(extra_ports=ports, host=FLAGS.host,
                                 connect=False, window_loc=(700, 50))

  create = sc_pb.RequestCreateGame(
      realtime=FLAGS.realtime, local_map=sc_pb.LocalMap(map_path=map_inst.path))
  create.player_setup.add(type=sc_pb.Participant)
  create.player_setup.add(type=sc_pb.Participant)

  controller = host_proc.controller
  controller.save_map(map_inst.path, map_inst.data(run_config))
  controller.create_game(create)

portpicker

A library to choose unique available network ports.

Apache-2.0
Latest version published 9 months ago

Package Health Score

80 / 100
Full package analysis

Similar packages