How to use the anyio.run function in anyio

To help you get started, we’ve selected a few anyio 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 clamor-py / anysocks / tests / test_connection.py View on Github external
def test_connection_5(self):
        async def test():
            try:
                async with open_connection('http://foo.bar/baz') as con:
                    self.assertIsInstance(con, WebSocketConnection)
            except ValueError:
                # ValueError tells us that our WebSocket URI
                # doesn't have a "ws" or "wss" scheme, so
                # everything is fine with this error.
                pass

        anyio.run(test)
github standy66 / purerpc / tests / test_client_server_codegen.py View on Github external
async def gen():
                        for _ in range(20):
                            await yield_(HelloRequest(name=data))
                    self.assertEqual(
                        [response.message for response in await self.async_iterable_to_list(
                            stub.SayHelloToMany(gen()))],
                        [data * 20]
                    )

                async def main():
                    async with purerpc.insecure_channel("localhost", port) as channel:
                        async with anyio.create_task_group() as task_group:
                            for _ in range(10):
                                await task_group.spawn(worker, channel)

                anyio.run(main)
github standy66 / purerpc / tests / test_client_server_metadata.py View on Github external
GreeterStub = grpc_module.GreeterStub
            async def worker(channel):
                stub = GreeterStub(channel)
                response = await stub.SayHello(HelloRequest(name="World"), metadata=metadata)
                received_metadata = pickle.loads(base64.b64decode(response.message))
                print("Server received metadata (in client)", received_metadata)
                self.assertEqual(received_metadata[0][0], "grpc-message-type")
                received_metadata = received_metadata[1:]
                self.assertEqual(metadata, received_metadata)


            async def main():
                async with purerpc.insecure_channel("localhost", port) as channel:
                    await worker(channel)
            anyio.run(main)
github standy66 / purerpc / tests / test_client_server_metadata.py View on Github external
)

                GreeterStub = grpc_module.GreeterStub

                async def worker(channel):
                    stub = GreeterStub(channel)
                    response = await stub.SayHello(HelloRequest(name="World"), metadata=metadata)
                    received_metadata = pickle.loads(base64.b64decode(response.message))
                    print("Server received metadata (in client)", received_metadata)
                    self.assertEqual(metadata, received_metadata)

                async def main():
                    async with purerpc.insecure_channel("localhost", port) as channel:
                        await worker(channel)

                anyio.run(main)
github standy66 / purerpc / tests / test_client_server_codegen.py View on Github external
self.assertEqual(
                    (await stub.SayHelloToManyAtOnce(name_generator())).message,
                    "Hello, Foo, Bar, Bat, Baz"
                )
                self.assertEqual(
                    [response.message for response in await self.async_iterable_to_list(
                        stub.SayHelloToMany(name_generator()))],
                    ["Hello, Foo", "Hello, Bar", "Hello, Bat", "Hello, Baz"]
                )

            async def main():
                async with purerpc.insecure_channel("localhost", port) as channel:
                    async with anyio.create_task_group() as task_group:
                        for _ in range(50):
                            await task_group.spawn(worker, channel)
            anyio.run(main)
github standy66 / purerpc / src / purerpc / test_utils.py View on Github external
import purerpc
        server = purerpc.Server(port=0)
        server.add_service(service)
        socket = server._create_socket_and_listen()
        yield socket.getsockname()[1]

        async def sleep_10_seconds_then_die():
            await anyio.sleep(20)
            raise ValueError

        async def main():
            async with anyio.create_task_group() as tg:
                await tg.spawn(server._run_async_server, socket)
                await tg.spawn(sleep_10_seconds_then_die)
        # import cProfile
        anyio.run(server._run_async_server, socket)
        # cProfile.runctx("anyio.run(main)", globals(), locals(), sort="tottime")
github standy66 / purerpc / misc / greeter / test_perf.py View on Github external
def target_fn(worker_id):
            queue = queues[worker_id]
            anyio.run(worker, port, queue, args.num_concurrent_streams,
                      args.num_requests_per_stream, args.num_rounds, args.message_size,
                      args.load_type)
github Fuyukai / curious / curious / groundwork / runner.py View on Github external
config = toml.loads(Path(file).read_text(encoding="utf-8"))
    groundwork_section = config["groundwork"]
    bot_class = groundwork_section["bot_class"]
    backend = groundwork_section.get("backend", "trio")

    # format: pkg.mod:BotClass
    # we split it out then getattr() it
    module, kls = bot_class.split(":")
    mod = importlib.import_module(module)
    bot_klass = getattr(mod, kls)

    async def async_runner():
        new_bot = bot_klass(config)
        return await new_bot.run_async()

    anyio.run(async_runner, backend=backend)
github standy66 / purerpc / misc / greeter / failing_client.py View on Github external
async with anyio.create_task_group() as task_group:
                for _ in range(num_concurrent_streams):
                    await task_group.spawn(load_fn, task_results, stub, num_requests_per_stream, message_size)
            end = time.time()

            rps = num_concurrent_streams * num_requests_per_stream / (end - start)

            latencies = []
            for _ in range(num_concurrent_streams):
                latencies.append(await task_results.get())

            print("Round", idx, "rps", rps, "avg latency", 1000 * sum(latencies) / len(latencies))


if __name__ == "__main__":
    anyio.run(worker, 50055, 100, 50, 10, 1000, "unary")
github sanitizers / octomachinery / octomachinery / app / server / runner.py View on Github external
server=WebServerConfig(*sys.argv[1:3]),
            )

    logging.basicConfig(
        level=logging.DEBUG
        if config.runtime.debug  # pylint: disable=no-member
        else logging.INFO,
    )
    if config.runtime.debug:  # pylint: disable=no-member
        logger.debug(
            ' App version: {!s} '.center(50, '='),
            config.github.app_version,
        )

    try:
        run_until_complete(run_server_forever, config, event_routers)
    except (GracefulExit, KeyboardInterrupt):
        logger.info(' Exiting the app '.center(50, '='))