本文整理汇总了Python中portpicker.pick_unused_port方法的典型用法代码示例。如果您正苦于以下问题:Python portpicker.pick_unused_port方法的具体用法?Python portpicker.pick_unused_port怎么用?Python portpicker.pick_unused_port使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类portpicker
的用法示例。
在下文中一共展示了portpicker.pick_unused_port方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_local_cluster
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def create_local_cluster(num_workers, num_ps, protocol="grpc"):
"""Create local GRPC servers and return them."""
worker_ports = [portpicker.pick_unused_port() for _ in range(num_workers)]
ps_ports = [portpicker.pick_unused_port() for _ in range(num_ps)]
cluster_dict = {
"worker": ["localhost:%s" % port for port in worker_ports],
"ps": ["localhost:%s" % port for port in ps_ports]}
cs = tf.train.ClusterSpec(cluster_dict)
workers = [
tf.train.Server(
cs, job_name="worker", protocol=protocol, task_index=ix, start=True)
for ix in range(num_workers)]
ps_servers = [
tf.train.Server(
cs, job_name="ps", protocol=protocol, task_index=ix, start=True)
for ix in range(num_ps)]
return workers, ps_servers
# Creates the workers and return their sessions, graphs, train_ops.
示例2: create_local_cluster
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def create_local_cluster(num_workers, num_ps, protocol="grpc"):
"""Create local GRPC servers and return their servers."""
worker_ports = [portpicker.pick_unused_port() for _ in range(num_workers)]
ps_ports = [portpicker.pick_unused_port() for _ in range(num_ps)]
cluster_dict = {
"worker": ["localhost:%s" % port for port in worker_ports],
"ps": ["localhost:%s" % port for port in ps_ports]}
cs = tf.train.ClusterSpec(cluster_dict)
workers = [
tf.train.Server(
cs, job_name="worker", protocol=protocol, task_index=ix, start=True)
for ix in range(num_workers)]
ps_servers = [
tf.train.Server(
cs, job_name="ps", protocol=protocol, task_index=ix, start=True)
for ix in range(num_ps)]
return workers, ps_servers
示例3: pick_unused_ports
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def pick_unused_ports(num_ports, retry_interval_secs=1, retry_attempts=5):
"""Reserves and returns a list of `num_ports` unused ports."""
if num_ports <= 0:
raise ValueError("Number of ports, must be >= 1, got: %s" % num_ports)
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)
示例4: pick_contiguous_unused_ports
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
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)
示例5: __init__
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def __init__(self, host: str = "127.0.0.1", port: Optional[int] = None, fullscreen: bool = False,
render: bool = False) -> None:
assert isinstance(host, str)
assert isinstance(port, int) or port is None
self._render = render
self._fullscreen = fullscreen
self._host = host
if port is None:
self._port = portpicker.pick_unused_port()
else:
self._port = port
self._tmp_dir = tempfile.mkdtemp(prefix="SC2_")
self._process = None
self._session = None
self._ws = None
示例6: setUp
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def setUp(self):
self._test_dir = tempfile.mkdtemp()
self.assertIsNotNone(self._test_dir)
self._server_port = portpicker.pick_unused_port()
self._compilation_server_proc = subprocess.Popen(
[os.path.join(
os.environ["TEST_SRCDIR"],
"__main__/freemindlatex/freemindlatex_app_main"),
"--port",
str(self._server_port),
"server"])
self._server_address = "127.0.0.1:{}".format(self._server_port)
self._compilation_client = compilation_client_lib.LatexCompilationClient(
self._server_address)
compilation_client_lib.WaitTillHealthy(self._server_address)
self.assertTrue(self._compilation_client.CheckHealthy())
示例7: Launch
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def Launch(debug: bool = False):
"""Launch dashboard in a separate thread."""
port = FLAGS.clgen_dashboard_port or portpicker.pick_unused_port()
app.Log(
1, "Launching CLgen dashboard on http://127.0.0.1:%d", port,
)
kwargs = {
"port": port,
# Debugging must be disabled when run in a separate thread.
"debug": debug,
"host": "0.0.0.0",
}
db.create_all()
if debug:
flask_app.run(**kwargs)
else:
thread = threading.Thread(target=flask_app.run, kwargs=kwargs)
thread.setDaemon(True)
thread.start()
return thread
示例8: setUp
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def setUp(self):
with mock.patch.object(bidaf_server.BidafServer,
'_InitializeEnvironment') as mock_method:
port = portpicker.pick_unused_port()
server_pool = logging_pool.pool(max_workers=10)
self._server = grpc.server(server_pool)
self._server.add_insecure_port('[::]:{}'.format(port))
servicer = bidaf_server.BidafServer('BidafServer', 'test BiDAF server')
servicer._environment = MockBidafEnvironment()
aqa_pb2_grpc.add_EnvironmentServerServicer_to_server(
servicer, self._server)
self._server.start()
channel = grpc.insecure_channel('localhost:%d' % port)
self._stub = aqa_pb2_grpc.EnvironmentServerStub(channel)
mock_method.assert_called_once_with(
model_dir=None,
data_dir=None,
debug_mode=False,
load_test=False,
load_impossible_questions=False,
shared_file=None)
示例9: load_from_docker
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def load_from_docker(settings, name=None):
"""Load Memory Tasks from docker container.
Args:
settings: EnvironmentSettings required to start the environment.
name: Optional name of Docker image that contains the dm_memorytasks
environment. If left unset, uses the dm_memorytasks default name.
Returns:
An implementation of dm_env.Environment
"""
_validate_environment_settings(settings)
name = name or _DEFAULT_DOCKER_IMAGE_NAME
client = docker.from_env()
port = portpicker.pick_unused_port()
try:
client.images.get(name)
except docker.errors.ImageNotFound:
logging.info('Downloading docker image "%s"...', name)
client.images.pull(name)
logging.info('Download finished.')
container = client.containers.run(
name,
auto_remove=True,
detach=True,
ports={_DOCKER_INTERNAL_GRPC_PORT: port})
return _MemoryTasksContainerEnv(
_connect_to_environment(port, settings), _MEMORY_TASK_OBSERVATIONS,
settings.num_action_repeats, container)
示例10: create_remote_executor
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def create_remote_executor():
port = portpicker.pick_unused_port()
channel = grpc.insecure_channel('localhost:{}'.format(port))
return remote_executor.RemoteExecutor(channel, 'REQUEST_REPLY')
示例11: test_context
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def test_context(rpc_mode='REQUEST_REPLY'):
port = portpicker.pick_unused_port()
server_pool = logging_pool.pool(max_workers=1)
server = grpc.server(server_pool)
server.add_insecure_port('[::]:{}'.format(port))
target_executor = executor_stacks.local_executor_factory(
num_clients=3).create_executor({})
tracer = executor_test_utils.TracingExecutor(target_executor)
service = executor_service.ExecutorService(tracer)
executor_pb2_grpc.add_ExecutorServicer_to_server(service, server)
server.start()
channel = grpc.insecure_channel('localhost:{}'.format(port))
remote_exec = remote_executor.RemoteExecutor(channel, rpc_mode)
executor = reference_resolving_executor.ReferenceResolvingExecutor(
remote_exec)
try:
yield collections.namedtuple('_', 'executor tracer')(executor, tracer)
finally:
executor.close()
tracer.close()
try:
channel.close()
except AttributeError:
pass # Public gRPC channel doesn't support close()
finally:
server.stop(None)
示例12: __init__
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def __init__(self, executor: executor_base.Executor):
port = portpicker.pick_unused_port()
server_pool = logging_pool.pool(max_workers=1)
self._server = grpc.server(server_pool)
self._server.add_insecure_port('[::]:{}'.format(port))
self._service = executor_service.ExecutorService(executor)
executor_pb2_grpc.add_ExecutorServicer_to_server(self._service,
self._server)
self._server.start()
self._channel = grpc.insecure_channel('localhost:{}'.format(port))
self._stub = executor_pb2_grpc.ExecutorStub(self._channel)
示例13: getUnusedPort
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def getUnusedPort():
"""Returns an unused TCP port on the local host inside the defined port range.
To be used when starting peer SAS webserver or other database webserver.
"""
config = _GetSharedTestConfig()
if config.min_port < 0:
return portpicker.pick_unused_port()
global _ports
# Find the first available port in the defined range.
for p in xrange(config.min_port, config.max_port):
if p not in _ports and portpicker.is_port_free(p):
_ports.add(p)
return p
raise AssertionError('No available new ports')
示例14: __init__
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def __init__(self):
self.shared = portpicker.pick_unused_port()
self.server = [portpicker.pick_unused_port() for _ in range(2)]
self.players = [[portpicker.pick_unused_port() for _ in range(2)] for _ in range(2)]
示例15: mock_distribute
# 需要导入模块: import portpicker [as 别名]
# 或者: from portpicker import pick_unused_port [as 别名]
def mock_distribute(fn, num_workers=2):
"""Runs `fn` in multiple processes, setting appropriate env vars."""
port = str(portpicker.pick_unused_port())
with mock.patch.object(os, 'environ', MockEnvVars()):
def chief_fn():
# The IP address of the chief Oracle. Run in distributed mode when
# present. Cloud oracle does not run in this mode because the Cloud
# API coordinates workers itself.
os.environ['KERASTUNER_ORACLE_IP'] = '127.0.0.1'
# The port of the chief Oracle.
os.environ['KERASTUNER_ORACLE_PORT'] = port
# The ID of this process. 'chief' will run the OracleServicer server.
os.environ['KERASTUNER_TUNER_ID'] = 'chief'
fn()
chief_thread = ExceptionStoringThread(target=chief_fn)
chief_thread.daemon = True
chief_thread.start()
worker_threads = []
for i in range(num_workers):
def worker_fn():
os.environ['KERASTUNER_ORACLE_IP'] = '127.0.0.1'
os.environ['KERASTUNER_ORACLE_PORT'] = port
# Workers that are part of the same multi-worker
# DistributionStrategy should have the same TUNER_ID.
os.environ['KERASTUNER_TUNER_ID'] = 'worker{}'.format(i)
fn()
worker_thread = ExceptionStoringThread(target=worker_fn)
worker_thread.start()
worker_threads.append(worker_thread)
for worker_thread in worker_threads:
worker_thread.join()
if chief_thread.raised_exception:
six.reraise(*chief_thread.raised_exception)
for worker_thread in worker_threads:
if worker_thread.raised_exception is not None:
six.reraise(*worker_thread.raised_exception)