Last active
May 31, 2023 18:00
-
-
Save hokiegeek2/93816b51f66643ae9f2f7eb86cd737fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # arkouda.proto | |
| syntax = "proto3"; | |
| package arkouda; | |
| service Arkouda { | |
| rpc HandleRequest(ArkoudaRequest) returns (ArkoudaResponse) {} | |
| } | |
| message ArkoudaRequest { | |
| string user=1; | |
| string token=2; | |
| string cmd=3; | |
| string format=4; | |
| string args=5; | |
| } | |
| message ArkoudaResponse { | |
| string message=1; | |
| } | |
| # Rust arkouda.toml: | |
| [package] | |
| name = "arkouda-tonic" | |
| version = "0.1.0" | |
| edition = "2021" | |
| [[bin]] # Bin to run the HelloWorld gRPC server | |
| name = "arkouda-server" | |
| path = "src/server.rs" | |
| [[bin]] # Bin to run the HelloWorld gRPC client | |
| name = "arkouda-client" | |
| path = "src/client.rs" | |
| [dependencies] | |
| tonic = "0.9" | |
| prost = "0.11" | |
| tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } | |
| [build-dependencies] | |
| tonic-build = "0.9" | |
| # build.rs | |
| fn main() -> Result<(), Box<dyn std::error::Error>> { | |
| tonic_build::compile_protos("proto/arkouda.proto")?; | |
| Ok(()) | |
| } | |
| # client.rs | |
| use arkouda::arkouda_client::ArkoudaClient; | |
| use arkouda::ArkoudaRequest; | |
| pub mod arkouda { | |
| tonic::include_proto!("arkouda"); | |
| } | |
| #[tokio::main] | |
| async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
| let mut client = ArkoudaClient::connect("http://[::1]:50051").await?; | |
| let request = tonic::Request::new(ArkoudaRequest { | |
| user: "Tonic".into(), | |
| token: "Tonic".into(), | |
| cmd: "Tonic".into(), | |
| format: "Tonic".into(), | |
| //size: "Tonic".into(), | |
| args: "Tonic".into(), | |
| }); | |
| let response = client.handle_request(request).await?; | |
| println!("RESPONSE={:?}", response); | |
| Ok(()) | |
| } | |
| # server.rs | |
| ``` | |
| use tonic::{transport::Server, Request, Response, Status}; | |
| use arkouda::arkouda_server::{Arkouda, ArkoudaServer}; | |
| use arkouda::{ArkoudaReply, ArkoudaRequest}; | |
| pub mod arkouda { | |
| tonic::include_proto!("arkouda"); | |
| } | |
| #[derive(Debug, Default)] | |
| pub struct ArkServer {} | |
| #[tonic::async_trait] | |
| impl Arkouda for ArkServer { | |
| async fn handle_request( | |
| &self, | |
| request: Request<ArkoudaRequest>, | |
| ) -> Result<Response<ArkoudaReply>, Status> { | |
| println!("Got an arkouda request: {:?}", request); | |
| let reply = arkouda::ArkoudaReply { | |
| message: format!("Reponse from Arkouda for the {} command", request.into_inner().cmd).into(), | |
| }; | |
| Ok(Response::new(reply)) | |
| } | |
| } | |
| ``` | |
| ``` | |
| #[tokio::main] | |
| async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
| let addr = "[::1]:50051".parse()?; | |
| let arkouda = ArkServer::default(); | |
| Server::builder() | |
| .add_service(ArkoudaServer::new(arkouda)) | |
| .serve(addr) | |
| .await?; | |
| Ok(()) | |
| } | |
| ``` | |
| # Running arkouda.server | |
| ``` | |
| cargo run --bin arkouda-server | |
| ``` | |
| # arkouda-client.py | |
| ``` | |
| from __future__ import print_function | |
| import logging | |
| import grpc | |
| import arkouda_pb2 | |
| import arkouda_pb2_grpc | |
| def run(): | |
| # NOTE(gRPC Python Team): .close() is possible on a channel and should be | |
| # used in circumstances in which the with statement does not fit the needs | |
| # of the code. | |
| print("Sending request to Arkouda gRPC ...") | |
| with grpc.insecure_channel('[::]:50051') as channel: | |
| stub = arkouda_pb2_grpc.ArkoudaStub(channel) | |
| response = stub.HandleRequest(arkouda_pb2.ArkoudaRequest(user='kjyost', | |
| token='notatoken', | |
| cmd='get_config', | |
| format='STRING', | |
| args='None')) | |
| print(f"Arkouda gRPC client received response") | |
| if __name__ == '__main__': | |
| logging.basicConfig() | |
| run() | |
| ``` | |
| # building Python arouda.proto | |
| ``` | |
| /usr/bin/python3.10 -m grpc_tools.protoc -I../../protos --python_out=. --pyi_out=. --grpc_python_out=. ../../protos/arkouda.proto | |
| ``` | |
| # arkouda-client.py | |
| ``` | |
| from __future__ import print_function | |
| import logging | |
| import grpc | |
| import arkouda_pb2 | |
| import arkouda_pb2_grpc | |
| def run(): | |
| # NOTE(gRPC Python Team): .close() is possible on a channel and should be | |
| # used in circumstances in which the with statement does not fit the needs | |
| # of the code. | |
| print("Sending request to Arkouda gRPC ...") | |
| with grpc.insecure_channel('[::]:50051') as channel: | |
| stub = arkouda_pb2_grpc.ArkoudaStub(channel) | |
| response = stub.HandleRequest(arkouda_pb2.ArkoudaRequest(user='kjyost', | |
| token='notatoken', | |
| cmd='get_config', | |
| format='STRING', | |
| args='None')) | |
| print(f"Arkouda gRPC client received response") | |
| if __name__ == '__main__': | |
| logging.basicConfig() | |
| run() | |
| ``` |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python-grpc-arkouda_server.mp4