예제 #1
0
async def main():
    channel = Channel('127.0.0.1', 50051)
    stub = ChallengeStub(channel)

    challenge = await stub.create(name='Mr. Easy')
    print(challenge.id, challenge.name)

    async for challenge in stub.list():
        print(challenge.id, challenge.name)

    channel.close()
예제 #2
0
async def main(rmq_host, rmq_port, rollout_size, max_dota_time,
               latest_weights_prob, initial_model, validation, log_dir):
    logger.info('main(rmq_host={}, rmq_port={})'.format(rmq_host, rmq_port))

    # RMQ
    rmq_connection = pika.BlockingConnection(
        pika.ConnectionParameters(host=rmq_host, port=rmq_port, heartbeat=300))
    experience_channel = rmq_connection.channel()
    experience_channel.queue_declare(queue=EXPERIENCE_QUEUE_NAME)

    weight_store.ready = asyncio.Event(loop=asyncio.get_event_loop())

    global writer
    writer = SummaryWriter(log_dir=log_dir)

    # Optionally
    if initial_model:
        weight_store.load_from_gcs(initial_model)

    # Set up the model callback.
    await setup_model_cb(host=rmq_host, port=rmq_port)

    # Wait for the first model weight to come in.
    await weight_store.ready.wait()

    # Connect to dota
    channel_dota = Channel(DOTASERVICE_HOST,
                           DOTASERVICE_PORT,
                           loop=asyncio.get_event_loop())
    dota_service = DotaServiceStub(channel_dota)

    game = Game(dota_service=dota_service,
                experience_channel=experience_channel,
                rollout_size=rollout_size,
                max_dota_time=max_dota_time,
                latest_weights_prob=latest_weights_prob,
                validation=validation)

    for i in range(0, N_GAMES):
        logger.info('=== Starting Game {}.'.format(i))
        game_id = str(datetime.now().strftime('%b%d_%H-%M-%S'))

        if validation:
            config = get_1v1_bot_vs_default_config(validation_team=validation)
        else:
            config = get_1v1_selfplay_config()

        try:
            await game.play(config=config, game_id=game_id)
        except:
            traceback.print_exc()
            return

    channel_dota.close()
예제 #3
0
파일: grpc.py 프로젝트: Vishalan/idb
 async def build(
         cls, host: str, port: int, is_local: bool,
         logger: logging.Logger) -> AsyncContextManager[IdbClientBase]:
     channel = Channel(host=host, port=port, loop=asyncio.get_event_loop())
     try:
         yield IdbClient(
             stub=CompanionServiceStub(channel=channel),
             is_local=is_local,
             logger=logger,
         )
     finally:
         channel.close()
예제 #4
0
async def run(parser: ArgumentParser, args: Namespace,
              command_cls: Type[ClientCommand]) -> int:
    loop = asyncio.get_event_loop()
    path = args.socket or AdminService.get_socket_path()
    channel = Channel(path=path, loop=loop)
    stub = AdminStub(channel)
    command = command_cls(stub, args)
    try:
        code = await command.run(args.outfile)
    finally:
        channel.close()
    return code
예제 #5
0
async def main(*, host: str = 'localhost', port: int = 50051) -> None:
    channel = Channel(host,
                      port,
                      ssl=create_secure_context(
                          CLIENT_CERT,
                          CLIENT_KEY,
                          trusted=SERVER_CERT,
                      ))
    stub = HealthStub(channel)
    response = await stub.Check(HealthCheckRequest())
    print(response)
    channel.close()
예제 #6
0
파일: __init__.py 프로젝트: icgood/pymap
async def run(parser: ArgumentParser, args: Namespace,
              command_cls: Type[ClientCommand]) -> int:
    loop = asyncio.get_event_loop()
    path = args.socket or AdminService.get_socket_path()
    channel = Channel(path=path, loop=loop)
    stub = AdminStub(channel)
    command = command_cls(stub, args)
    try:
        code = await command.run(args.outfile)
    finally:
        channel.close()
    return code
예제 #7
0
class Protoconf(object):
    def __init__(self, host="127.0.0.1", port=AGENT_DEFAULT_PORT):
        self._host = host
        self._port = port
        self._clear_state()

    def _clear_state(self):
        self._channel = None
        self._streams = []
        self._updates_tasks = []

    async def get_and_subscribe(self, path, protobuf_type, callback):
        if self._channel == None:
            self._channel = Channel(self._host, self._port)

        stream = (await ProtoconfServiceStub(
            self._channel).SubscribeForConfig.open().__aenter__())
        await stream.send_message(ConfigSubscriptionRequest(path=path))

        async def get_config():
            protoconf_value = await stream.recv_message()
            if protoconf_value == None:
                return None

            config = protobuf_type()
            protoconf_value.value.Unpack(config)
            return config

        config = await get_config()
        if config != None and callback != None:
            self._streams.append(stream)

            async def get_updates():
                while True:
                    config = await get_config()
                    if config == None:
                        break
                    asyncio.ensure_future(callback(config))

            self._updates_tasks.append(asyncio.ensure_future(get_updates()))
        else:
            await stream.cancel()
        return config

    async def close(self):
        for stream in self._streams:
            await stream.cancel()
            await stream.__aexit__(None, None, None)
        for task in self._updates_tasks:
            task.cancel()
        self._channel.close()
        self._clear_state()
예제 #8
0
    async def call(self, operation, address, vnfbr):
        vnfbr_pb = vnfbr.protobuf()
        layout = Layout(feat=operation)
        layout.vnfbr.CopyFrom(vnfbr_pb)

        ip, port = address.split(":")
        channel = Channel(ip, port)
        stub = PlayerStub(channel)

        reply, error = await self.call_stub(stub.CallLayout, layout)

        channel.close()
        return reply, error
예제 #9
0
파일: client.py 프로젝트: twds/grpclib
async def main() -> None:
    channel = Channel('127.0.0.1', 50051)
    primes = PrimesStub(channel)

    async def check(n: int) -> Tuple[int, bool]:
        reply = await primes.Check(Request(number=n))
        return n, reply.is_prime.value

    for f in asyncio.as_completed([check(n) for n in PRIMES]):
        number, is_prime = await f
        print(f'Number {number} {"is" if is_prime else "is not"} prime')

    channel.close()
예제 #10
0
    async def call_task(self, uuid, task):
        """Calls a task in a Manager component
        using a gRPC stub

        Arguments:
            uuid {string} -- The uuid of a Manager component
            that is a peer of Player and by whom the Task will
            be executed
            task {Task} -- A gRPC message of type Task that
            the Manager component being called will have to
            execute

        Returns:
            dict -- All the information of a Report message
            obtained from a Manager component after running
            the called Task message
        """
        logger.info(f"Calling test task at manager uuid {uuid}")
        logger.debug(f"{json_format.MessageToJson(task)}")

        peers = self.status.get_peers("role", "manager")
        peer = peers.get(uuid)
        address = peer.get("address")
        host, port = address.split(":")
        channel = Channel(host, port)

        report_msg = Report(id=task.id, test=task.test)

        try:
            stub = ManagerStub(channel)
            report_msg = await stub.CallTask(task)

        except GRPCError as e:
            logger.info(f"Error in task call")
            logger.debug(f"{repr(e)}")
            report_msg = Report(id=task.id, test=task.test, error=repr(e))

        except OSError as e:
            logger.info(f"Error in channel for task call")
            logger.debug(f"{repr(e)}")
            report_msg = Report(id=task.id, test=task.test, error=repr(e))

        else:
            logger.debug(f"Report received")

        finally:
            logger.debug(f"{json_format.MessageToJson(report_msg)}")
            report = json_format.MessageToDict(report_msg)
            channel.close()

        return report
예제 #11
0
async def main():
    # Create a gRPC channel + await daq_service.
    channel = Channel(host=server_address, port=server_port)
    daq_service = nidaqmx_grpc.NiDAQmxStub(channel)
    task = None

    # Raise an exception if an error was returned
    async def raise_if_error(response):
        if response.status != 0:
            response = await daq_service.get_error_string(
                error_code=response.status)
            raise Exception(f"Error: {response.error_string}")

    try:
        response = await daq_service.create_task()
        await raise_if_error(response)
        task = response.task

        await raise_if_error(await daq_service.create_d_i_chan(
            task=task,
            lines=lines,
            line_grouping_raw=nidaqmx_grpc.LineGrouping.
            LINE_GROUPING_CHAN_FOR_ALL_LINES,
        ))

        await raise_if_error(await daq_service.start_task(task=task))

        response = await daq_service.read_digital_u32(
            task=task,
            num_samps_per_chan=1,
            array_size_in_samps=1,
            fill_mode_raw=nidaqmx_grpc.GroupBy.GROUP_BY_GROUP_BY_CHANNEL,
            timeout=10.0,
        )
        await raise_if_error(response)

        print(f"Data acquired: {hex(response.read_array[0])}")
    except GRPCError as e:
        if e.status.name == "UNIMPLEMENTED":
            print(
                "The operation is not implemented or is not supported/enabled in this service"
            )
        else:
            print(f"GRPCError: {str(e)}")
    except Exception as e:
        print(str(e))
    finally:
        if task:
            await daq_service.stop_task(task=task)
            await daq_service.clear_task(task=task)
        channel.close()
예제 #12
0
async def benchmark(loop,
                    seconds=DEFAULT_SECONDS,
                    concurrency=DEFAULT_CONCURRENCY):
    global finish_benchmark

    print("Creating stubs and warmming up ....")
    stubs = []
    channels = []
    for i in range(concurrency):
        channel = Channel('127.0.0.1', 50051, loop=loop)
        echo_stub = echo_grpc.EchoStub(channel)
        echo_reply = await echo_stub.Hi(echo_pb2.EchoRequest(message="ping"))
        assert echo_reply.message
        stubs.append(echo_stub)
        channels.append(channel)

    print("Starting tasks ....")
    tasks = [
        asyncio.ensure_future(requests(idx, echo_stub))
        for idx, echo_stub in enumerate(stubs)
    ]

    await asyncio.sleep(seconds)

    print("Finishing tasks ....")
    finish_benchmark = True

    while not all([task.done() for task in tasks]):
        await asyncio.sleep(0)

    for channel in channels:
        channel.close()

    times = []
    for task in tasks:
        times += task.result()

    times.sort()

    total_requests = len(times)
    avg = sum(times) / total_requests

    p75 = times[int((75 * total_requests) / 100)]
    p90 = times[int((90 * total_requests) / 100)]
    p99 = times[int((99 * total_requests) / 100)]

    print('QPS: {0}'.format(int(total_requests / seconds)))
    print('Avg: {0:.6f}'.format(avg))
    print('P75: {0:.6f}'.format(p75))
    print('P90: {0:.6f}'.format(p90))
    print('P99: {0:.6f}'.format(p99))
예제 #13
0
    async def run_instruction(self):
        uuid, address = "agent-test", "127.0.0.1:50051"
        p = self.start_agent(uuid, address)

        await asyncio.sleep(1.0)
        channel = Channel("127.0.0.1", 50051)
        stub = gym_grpc.AgentStub(channel)
        instruction_reply = await self.call_instruction(stub)
        channel.close()

        ack = self.stop_agent(p)
        assert ack == True

        return instruction_reply
예제 #14
0
async def main():
    channel = Channel(loop=asyncio.get_event_loop(),
                      host=settings.GRPC_SERVER_HOST,
                      port=settings.GRPC_SERVER_PORT)
    stub = BillingServiceStub(channel)

    reply = await stub.Register(
        RegisterRequest(name='jackdoe',
                        country='Russia',
                        city='Kursk',
                        currency='EUR'))
    print("REPLY HERE", reply, type(reply))

    channel.close()
예제 #15
0
async def run():
    channel = Channel(host='padplus.juzibot.com', port=50051)
    puppet_stub = PadPlusServerStub(channel)
    async for response in puppet_stub.init(token=token):
        # 一直卡在这里,没有收到消息
        # 所以怎么来获取事件,登录呢?这里有点疑惑
        print(response)

    # 这里应该是需要有一定的信息从init响应中返回
    response = await puppet_stub.request(
        RequestObject(token=token, api_type=ApiType.INIT))
    print(response)

    channel.close()
예제 #16
0
def channel_fixture(loop, port):
    services = [DummyService()]
    services = ServerReflection.extend(services)

    server = Server(services, loop=loop)
    loop.run_until_complete(server.start(port=port))

    channel = Channel(port=port, loop=loop)
    try:
        yield channel
    finally:
        channel.close()
        server.close()
        loop.run_until_complete(server.wait_closed())
예제 #17
0
 async def _companion_to_target(
         self, companion: CompanionInfo) -> Optional[TargetDescription]:
     try:
         channel = Channel(companion.host,
                           companion.port,
                           loop=asyncio.get_event_loop())
         stub = CompanionServiceStub(channel=channel)
         response = await stub.describe(TargetDescriptionRequest())
         channel.close()
         return target_to_py(response.target_description)
     except Exception:
         self.logger.warning(f"Failed to describe {companion}, removing it")
         self.direct_companion_manager.remove_companion(
             Address(companion.host, companion.port))
         return None
예제 #18
0
파일: client.py 프로젝트: su-chang/grpc
async def main():
    """doc"""
    channel = Channel(host="127.0.0.1", port=8788)
    puppet = PuppetStub(channel)

    event_stream = AsyncIOEventEmitter()
    event_stream.on('EVENT_TYPE_DONG', \
        lambda payload: print('on(dong) %s' % payload))

    await asyncio.gather(
        loop(lambda: puppet.ding(data='haha')),
        init_event_stream(event_stream, puppet),
    )

    channel.close()
예제 #19
0
    async def run_info(self):
        uuid, address = "monitor-test", "127.0.0.1:50051"

        p = self.start_monitor(uuid, address)

        await asyncio.sleep(1.0)
        channel = Channel("127.0.0.1", 50051)
        stub = gym_grpc.MonitorStub(channel)
        info_reply = await self.call_info(stub)
        channel.close()

        ack = self.stop_monitor(p)
        assert ack == True

        return info_reply
예제 #20
0
async def speech_stream_to_text(speech_stream):
    channel = Channel('stt.api.cloud.yandex.net', 443, ssl=True)
    try:
        stub = SttServiceStub(channel)
        sent = asyncio.Event()
        async with stub.StreamingRecognize.open(
                metadata=[('authorization',
                           get_authorization_header())]) as stream:
            async with background_task(
                    write_to_stream(stream, speech_stream, sent)):
                await sent.wait()
                async for text in read_from_stream(stream):
                    yield text
    finally:
        channel.close()
예제 #21
0
async def main(*, host: str, port: int):
    chan = Channel(host=host, port=port)
    player = PlayerServiceStub(chan)

    async for res in player.open(
            screen_path=screen_path,
            video_path=video_path,
            vstream_idx=0,
            frame_wait=frame_wait,
            start=start,
            step=step,
    ):
        print(res.action, res.ok, res.err, res.frame_count)

    chan.close()
    print("closed")
예제 #22
0
파일: grpc.py 프로젝트: page66688/idb
 async def build(
         cls, companion_info: CompanionInfo,
         logger: logging.Logger) -> AsyncContextManager["GrpcStubClient"]:
     channel = Channel(
         host=companion_info.host,
         port=companion_info.port,
         loop=asyncio.get_event_loop(),
     )
     try:
         yield GrpcStubClient(
             stub=CompanionServiceStub(channel=channel),
             companion_info=companion_info,
             logger=logger,
         )
     finally:
         channel.close()
예제 #23
0
    async def run_info(self):
        roles = ["agent", "monitor", "manager"]

        ps = self.start_components(roles)

        await asyncio.sleep(2.0)

        channel = Channel("127.0.0.1", 50053)
        stub = gym_grpc.ManagerStub(channel)
        info_reply = await self.call_info(stub)
        channel.close()

        ack = self.stop_components(ps)
        assert ack == True

        return info_reply
예제 #24
0
    async def call_peer(self, role, address, instruction):
        """Performs the call of a instruction in a agent/monitor
        peer.

        Arguments:
            role {string} -- The role of the peer being called
            (i.e., agent or monitor)
            address {string} -- The address (ip:port) of the peer
            being called
            instruction {Instruction} -- A gRPC message of type Instruction

        Returns:
            Snapshot -- A gRPC message of type Snapshot
            if not exceptions are raised because of grpc error
            or os error
        """
        reply = Snapshot(id=instruction.id)

        host, port = address.split(":")
        channel = Channel(host, port)

        if role == "agent":
            stub = AgentStub(channel)
        elif role == "monitor":
            stub = MonitorStub(channel)
        else:
            stub = None
            logger.info(
                f"Could not contact role {role} - no stub/client available")
            raise (Exception(f"No stub/client available for {role}"))

        try:
            reply = await stub.CallInstruction(instruction)

        except GRPCError as e:
            logger.info(f"Error in instruction call at {address}")
            logger.debug(f"Exception: {repr(e)}")
            raise (e)

        except OSError as e:
            logger.info(
                f"Could not open channel for instruction call at {address}")
            logger.debug(f"Exception: {repr(e)}")
            raise (e)

        channel.close()
        return reply
예제 #25
0
class Client:
    def __init__(self, host='127.0.0.1', port=2379, *, ttl=10):
        self._channel = Channel(host, port)

        self._kvstub = _etcd.KVStub(self._channel)
        self._leasestub = _etcd.LeaseStub(self._channel)
        self._watchstub = _etcd.WatchStub(self._channel)

        from .lease import Lease
        self._session = Lease(ttl, client=self)

    def __del__(self):
        self._channel.close()

    async def start_session(self):
        await self._session.grant()

    @property
    def session_id(self):
        return self._session.id

    def Delete(self, *args, **kwargs):
        return request.Delete(*args, **kwargs, client=self)

    def DeleteRange(self, *args, **kwargs):
        return request.DeleteRange(*args, **kwargs, client=self)

    def Get(self, *args, **kwargs):
        return request.Get(*args, **kwargs, client=self)

    def LeaseGrant(self, *args, **kwargs):
        return request.LeaseGrant(*args, **kwargs, client=self)

    def LeaseKeepAlive(self, *args, **kwargs):
        return request.LeaseKeepAlive(*args, **kwargs, client=self)

    def LeaseRevoke(self, *args, **kwargs):
        return request.LeaseRevoke(*args, **kwargs, client=self)

    def Put(self, *args, **kwargs):
        return request.Put(*args, **kwargs, client=self)

    def Range(self, *args, **kwargs):
        return request.Range(*args, **kwargs, client=self)

    def Txn(self, *args, **kwargs):
        return request.Txn(*args, **kwargs, client=self)
예제 #26
0
async def test_channel_transient_failure(loop):
    channel = Channel()

    async def _create_connection():
        await asyncio.sleep(0.01)
        raise asyncio.TimeoutError('try again later')

    with patch.object(channel, '_create_connection', _create_connection):
        assert channel._state is _ChannelState.IDLE
        connect_task = loop.create_task(channel.__connect__())
        await asyncio.sleep(0.001)
        assert channel._state is _ChannelState.CONNECTING
        with pytest.raises(asyncio.TimeoutError, match='try again later'):
            await connect_task
        assert channel._state is _ChannelState.TRANSIENT_FAILURE

    channel.close()
    assert channel._state is _ChannelState.IDLE
예제 #27
0
async def test_channel_ready(loop):
    async with ChannelFor([WorkingDummyService()]) as _channel:
        channel = Channel()

        async def _create_connection():
            await asyncio.sleep(0.01)
            return _channel._protocol

        with patch.object(channel, '_create_connection', _create_connection):
            assert channel._state is _ChannelState.IDLE
            connect_task = loop.create_task(channel.__connect__())
            await asyncio.sleep(0.001)
            assert channel._state is _ChannelState.CONNECTING
            await connect_task
            assert channel._state is _ChannelState.READY

        channel.close()
        assert channel._state is _ChannelState.IDLE
예제 #28
0
class Connection:
    '''
    An active connection to a model serving GRPC server
    '''

    TIMEOUT_SECONDS = 5

    def __init__(
            self,
            addr: str,
            port: int,
            pem: str = None,
            channel_options: dict = None,
            loop: asyncio.AbstractEventLoop = None,
        ):

        self.addr = addr
        self.port = port

        if channel_options is None:
            channel_options = {}
        if pem is None:
            make_sync_channel = partial(grpc.insecure_channel, options=channel_options)
        else:
            creds = grpc.ssl_channel_credentials(pem)
            make_sync_channel = partial(
                grpc.secure_channel,
                credentials=creds,
                options=channel_options,
            )

        if loop is None:
            loop = asyncio.get_event_loop()

        self.sync_channel = make_sync_channel(f"{addr}:{port}")
        self.async_channel = Channel(addr, port, loop=loop)

        self.sync_stub = prediction_service_pb2_grpc.PredictionServiceStub(self.sync_channel)
        self.async_stub = prediction_service_grpc.PredictionServiceStub(self.async_channel)

    def __del__(self):
        self.async_channel.close()
        del self.async_channel
예제 #29
0
class Client:
    def __init__(self, loop):
        self.channel = Channel("127.0.0.1", 5000, loop=loop)
        self.stub = ssStub(self.channel)

    async def get_user(self, user_id: int):
        user = await self.stub.GetUser(UserIdReq(user_id=1))
        print(f"user: {user}")

    async def list_user(self, tcp_conn_num):
        res = await self.stub.ListUser(UserReq(tcp_conn_num=tcp_conn_num))
        print(f"list_user: {res}")

    async def healcheck(self, url: str):
        res = await self.stub.HealthCheck(HealthCheckReq(url=url))
        print(f"health: {res}")

    def close(self):
        self.channel.close()
예제 #30
0
    async def call_monitor_event(self, scenario):
        monitor_events = scenario.get("eventsv2").get("monitor")

        # extract all the actions from monitor_events to
        # construct the Instruction message
        monitor_actions = []
        for ev in monitor_events:
            for action in ev.get("actions"):
                monitor_actions.append(action)

        instr_dict = {"id": scenario.get("id"), "actions": monitor_actions}

        ip, port = self.scenario.entrypoint.get("umbra-monitor").split(':')
        channel = Channel(ip, int(port))
        stub = MonitorStub(channel)

        instruction = json_format.ParseDict(instr_dict, Instruction())
        reply = await stub.Listen(instruction)
        channel.close()
예제 #31
0
async def run(parser: ArgumentParser, args: Namespace,
              command_cls: type[Command]) -> int:
    config = Config(args)
    ssl = create_default_context(cafile=config.cafile, capath=config.capath)
    if config.no_verify_cert:
        ssl.check_hostname = False
        ssl.verify_mode = CERT_NONE
    if config.host is not None or config.port is not None:
        channel = Channel(host=config.host, port=config.port, ssl=ssl)
    else:
        path = socket_file.find()
        if path is None:
            parser.error('Running server not found, please provide '
                         '--host, --port, or --path.')
        channel = Channel(path=str(path))
    command = command_cls(args, channel)
    try:
        return await command(sys.stdout)
    finally:
        channel.close()