gRPC is a powerful, high-performance Remote Procedure Call (RPC) framework that, despite being less commonly used than REST, offers significant advantages in certain scenarios.
In addition it's language agnostic and can run in any environment, making it an ideal choice for server-to-server communication.
I will not delve into in whole explenation of it but here is a general link of gRPC. I'll provide a hands on turtorial
Go gRPC client
Lets image our Go is client but is a server asfor frontend app React, Svelte etc.
func getFirstArg() (string, error) { if len(os.Args) <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172432455542554.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="gRPC Communication Between Go and Python"></p> <hr> <p>As an example React frontend uploads a file, Go process it but we need answers from excel we will use GPT API. While it can be done with Go, Python on the otherhand has more packages that can ease our lives like langchan_openai, pandas for excel and so forth.</p> <hr> <p>Lets start with instalation of gRPC preferably in your virtualenv .venv<br> </p> <pre class="brush:php;toolbar:false">$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest $ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest $ export PATH="$PATH:$(go env GOPATH)/bin"
Next up you should install protocol buffer in your OS can follow it here.
Let's create a proto dir where you will store your protocol buffer file I will name it as excel.proto and paste this:
syntax = "proto3"; option go_package = "client-gRPC/proto"; service ExcelService { rpc UploadFile(FileRequest) returns (FileResponse); } message FileRequest { string file_name = 1; bytes file_content = 2; } message FileResponse { bytes file_content = 1; }
This gRPC service, ExcelService, allows clients to upload a file by sending its name and content. The server responds with the same file content.
For Go its essential to pass in go_package in Python the line is not needed.
vscode-proto3 is a good extension to download if you use VSCode.
After all of this you can generate your proto files I preffer it in same level as prot dir, for that run this command:
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/excel.proto
If succesfull two files should be generated, optionally if there would be a lot of adjustments add a Makefile and define it as proto + upper command.
import ( .... "google.golang.org/grpc" pb "client-gRPC/proto" "github.com/xuri/excelize/v2" ) func main() { .... conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure()) if err != nil { log.Fatalf("Failed to connect to gRPC server: %v", err) } defer conn.Close() client := pb.NewExcelServiceClient(conn) req := &pb.FileRequest{ FileName: filePath, FileContent: fileData, } res, err := client.UploadFile(context.Background(), req) if err != nil { log.Fatalf("Failed to upload file: %v", err) } outputFile := "output.xlsx" err = saveBytesAsExcel(outputFile, res.FileContent) if err != nil { log.Fatalf("Failed to save bytes as Excel file: %v", err) } fmt.Printf("Excel file saved as: %s\n", outputFile) } func saveBytesAsExcel(filePath string, fileContent []byte) error { f, err := excelize.OpenReader(bytes.NewReader(fileContent)) if err != nil { return fmt.Errorf("failed to open Excel file: %v", err) } if err := f.SaveAs(filePath); err != nil { return fmt.Errorf("failed to save Excel file: %v", err) } return nil }
We make a connection to listen to 50051 that will be our Python server, &pb.FileRequest was generated prior by using proto command and now we are importing the methods. If you run you will recive ? due to Python server not established yet.
Failed to upload file: rpc error: code = Unavailable desc = connection error: desc = "transport: Error while dialing: dial tcp 127.0.0.1:50051: connect: connection refused"
Python gRPC server
As python will act as a server the approach will be slightly different but in essense same proto file appart from package field is not reqired. Lets start by creating a base main.py without the gRPC just to give a glance of how GPT will populate the questions in excel.
import os import openai import pandas as pd from dotenv import load_dotenv def get_answer_from_gpt(apikey: str, question: str): openai.api_key = apikey response = openai.ChatCompletion.create( model="gpt-4", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": question} ] ) return response['choices'][0]['message']['content'].strip() def answer_questions_df(df: pd.DataFrame, apikey: str): answers = [] for question in df.iloc[:, 0]: answer = get_answer_from_gpt(apikey, question) answers.append(answer) return answers if __name__ == "__main__": load_dotenv() openai_api_key = os.getenv("OPENAI_API_KEY", "OpenAI API key hasn't been set.") df = pd.read_excel('Book1.xlsx') df['Answer'] = answer_questions_df(df, openai_api_key
Its a simple script that will answer questions that Go will send us but the LOC is less due to dedicated openai library that makes it easier.
We start by as well adding proto dir with same file as above the option section can be removed as disccused. Install gRPC in your virtualenv preferably and follow here the instalation for proto generation I ran"
python3 -m grpc_tools.protoc --proto_path=proto --python_out=proto --grpc_python_out=proto proto/excel.proto
To be in same lvl as my proto directory remember to add __init.py!
Ones the files have been generated lets continue on.
import io import grpc from proto import excel_pb2_grpc as excel_grpc from proto import excel_pb2 class ExcelService(excel_grpc.ExcelServiceServicer): def UploadFile(self, request, context): try: # Convert bytes to a file-like object file_like_object = io.BytesIO(request.file_content) # Load the workbook from the file-like object workbook = openpyxl.load_workbook(file_like_object) # Access the first sheet (or use appropriate logic to get the sheet you need) sheet = workbook.active # Convert the sheet to a DataFrame data = sheet.values columns = next(data) # Get the header row df = pd.DataFrame(data, columns=columns) print("Loaded DataFrame:") print(df.head()) # Ensure that the DataFrame is not empty and has questions if df.empty or df.shape[1] <p>We define server and add ExcelService class what containes the methods generated by proto file. Because we recive file by bytes have to use io byte reader and commence further processing of the file and population the second column.<br> </p> <pre class="brush:php;toolbar:false">response = excel_pb2.FileResponse(file_content=output.read())
At the end we are returning ☝️ for our Go client to recive.
To be able to find proto files in python however you should define an export path
export PYTHONPATH=$PYTHONPATH:mnt/c/own_dev/gRPC/server/proto
Running Client and Server
If all is good you can run #First comes server python3 -m main #Then client go run client.go Book1.xlsx
And you should get the updated .xlsx file in Go client side.
Conclusion
In this article we explored the fundamentals of setting up gRPC communication between Python server and Go client. By leveraging gRPC, we established a seamless way to send an Excel file from a Go application to a Python server, process the file using OpenAI's GPT API, and return the modified file back to the Go client.
The above is the detailed content of gRPC Communication Between Go and Python. For more information, please follow other related articles on the PHP Chinese website!

The shortcut keys for customizing LibOffice on Debian systems can be adjusted through system settings. Here are some commonly used steps and methods to set LibOffice shortcut keys: Basic steps to set LibOffice shortcut keys Open system settings: In the Debian system, click the menu in the upper left corner (usually a gear icon), and select "System Settings". Select a device: In the system settings window, select "Device". Select a keyboard: On the Device Settings page, select Keyboard. Find the command to the corresponding tool: In the keyboard settings page, scroll down to the bottom to see the "Shortcut Keys" option. Clicking it will bring a window to a pop-up. Find the corresponding LibOffice worker in the pop-up window

When deploying a Kubernetes (K8s) cluster on a Debian system, multiple key points need to be paid attention to to ensure the stability and security of the cluster. Here are some major notes: Disable Swap partition: Starting with Kubernetes 1.8, Swap partition needs to be disabled. Swap can be temporarily disabled using the following command: sudoswapoff-a To permanently disable Swap, edit the /etc/fstab file and comment out the line containing "swap". Set kernel parameters: Enable IPv4 forwarding: sudotee/etc/sysctl.d/k8s.conf Set network parameters, such as net.bridge.brid

Kubernetes (K8s for short) has the following advantages to deploying on Debian: Stability: Debian is a stable and reliable operating system suitable for Kubernetes operating environment. Many tutorials recommend using Debian12 as the underlying operating system for Kubernetes deployment, which shows that Debian provides a reliable operating environment that can meet the basic requirements of Kubernetes for operating systems. Security: Debian provides powerful security features such as SELinux and AppArmor, which can further enhance the security of Kubernetes clusters. Through reasonable configuration and optimization measures, Kuberne can be ensured

Deploying a Kubernetes cluster on a Debian system can be achieved in a variety of ways. Here are the detailed steps to set up a Kubernetes cluster on Debian12 using the kubeadm tool: Preparing to make sure your Debian system has been updated to the latest version. Make sure you have sudo users with administrator privileges. Ensure that all nodes can be connected to each other through a stable network. Installation steps: Set the host name and update the hosts file: On each node, use the hostnamectl command to set the host name, and add the corresponding relationship between the node IP and the host name in the /etc/hosts file. Disable swap partitions for all nodes: in order to make kubelet

To build a Golang environment on the Debian system, you can follow the following steps: 1. Update the system package list First, make sure that your system package list is the latest: sudoaptupdate2. The official repository for installing GolangDebian provides Golang installation packages. You can use the following command to install: sudoaptinstallgolang-go3. Verify that after the installation is completed, you can verify that Golang is successfully installed through the following command: If the installation is successful, you will see an output similar to the following: governversiongo1.20.3linux/amd644. Set environment change

When developing JavaScript on Debian systems, you can use the following best practices to optimize the development process: Choosing the right log library is crucial for Node.js applications, choosing a powerful log library. Commonly used log libraries such as Winston, Pino and Bunyan provide rich functions, including log-level settings, formatting and storage. Using the correct log level Use the log level correctly (such as fatal, error, warn, info, debug) can help distinguish between critical events and general information events, and help with subsequent troubleshooting and performance optimization. Log analysis tool GoAccess: For network log analysis, GoAccess is a

The steps to update the Kubernetes version on Debian are as follows: Backup an existing cluster: Make sure to back up your Kubernetes cluster data before any upgrades. This can be done by using etcd's backup tool. Check the current version: First, you need to know which version of Kubernetes is currently running. You can check it with the following command: kubectlversion to view available updates: Visit the official Kubernetes release page (https://github.com/kubernetes/kubernetes/releases), view the latest stable version, and confirm whether your Debian is supported

The reflection mechanism of Go language is implemented through the reflect package, providing the ability to check and manipulate arbitrary types of values, but it will cause performance problems. 1) The reflection operation is slower than the direct operation and requires additional type checking and conversion. 2) Reflection will limit compiler optimization. 3) Optimization methods include reducing reflection usage, caching reflection results, avoiding type conversions and paying attention to concurrency security.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
