Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
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)
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)
)
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)
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)
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")
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)
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)
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")
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, '='))