I am currently trying to implement grpc with flask (grpc as a server and flask as the client side) but I keep facing this issue: ModuleNotFoundError: No module named 'queue_pb2' when I am trying to run the command 'flask run' in the terminal.
This is my current directory structure:
Cloud/
|-- app.py
|-- microservices/
|-- queue/
|-- protos/
|-- queue.proto
|-- queue_pb2.py
|-- queue_pb2_grpc.py
This is the command I used in my terminal to generate the pb2 files in the microservices/queue directory
python -m grpc_tools.protoc -I protos --python_out=. --pyi_out=. --grpc_python_out=. protos/queue.proto
This is my current code from app.py
from flask import Flask, render_template, redirect, url_for, flash, request
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
import grpc
from microservices.queue.queue_pb2 import CheckInResponse, CheckOutResponse, CleanupQueueResponse, GetQueueCountResponse, CheckInRequest, CheckOutRequest, CleanupQueueRequest, GetQueueCountRequest
from microservices.queue.queue_pb2_grpc import QueueStub
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'xxx'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///../db/mall.db'
db = SQLAlchemy(app)
login_manager = LoginManager(app)
# Create Queue table
class Queue(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
check_in = db.Column(db.DateTime, default=datetime.utcnow)
@classmethod
def cget_user_by_username(cls, username):
return cls.query.filter_by(username=username).first()
@classmethod
def add_user(cls, username, check_in_time):
new_user = cls(username=username, check_in=check_in_time)
db.session.add(new_user)
db.session.commit()
@classmethod
def remove_user(cls, user):
db.session.delete(user)
db.session.commit()
@classmethod
def cleanup_queue(cls, threshold_time):
cls.query.filter(cls.check_in <= threshold_time).delete()
db.session.commit()
@classmethod
def get_queue_count(cls):
return cls.query.count()
This is my queue.proto
syntax = "proto3";
package queue;
message CheckInRequest {
string username = 1;
}
message CheckInResponse {
string message = 1;
}
message CheckOutRequest {
string username = 1;
}
message CheckOutResponse {
string message = 1;
}
message CleanupQueueRequest {}
message CleanupQueueResponse {
string message = 1;
}
message GetQueueCountRequest {}
message GetQueueCountResponse {
int32 count = 1;
}
service Queue {
rpc CheckIn (CheckInRequest) returns (CheckInResponse);
rpc CheckOut (CheckOutRequest) returns (CheckOutResponse);
rpc CleanupQueue (CleanupQueueRequest) returns (CleanupQueueResponse);
rpc GetQueueCount (GetQueueCountRequest) returns (GetQueueCountResponse);
}
This is a snippet of the queue_pb2_grpc.py as you can see the line import queue_pb2 as queue__pb2 is there
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc
import queue_pb2 as queue__pb2
class QueueStub(object):
"""Missing associated documentation comment in .proto file."""
def __init__(self, channel):
"""Constructor.
Args:
channel: A grpc.Channel.
"""
self.CheckIn = channel.unary_unary(
'/queue.Queue/CheckIn',
request_serializer=queue__pb2.CheckInRequest.SerializeToString,
response_deserializer=queue__pb2.CheckInResponse.FromString,
)
self.CheckOut = channel.unary_unary(
'/queue.Queue/CheckOut',
request_serializer=queue__pb2.CheckOutRequest.SerializeToString,
response_deserializer=queue__pb2.CheckOutResponse.FromString,
)
self.CleanupQueue = channel.unary_unary(
'/queue.Queue/CleanupQueue',
request_serializer=queue__pb2.CleanupQueueRequest.SerializeToString,
response_deserializer=queue__pb2.CleanupQueueResponse.FromString,
)
self.GetQueueCount = channel.unary_unary(
'/queue.Queue/GetQueueCount',
request_serializer=queue__pb2.GetQueueCountRequest.SerializeToString,
response_deserializer=queue__pb2.GetQueueCountResponse.FromString,
)
Any advice would be appreciated as I have been stuck on this for quite some time thank you!
Revise this import statement
import queue_pb2 as queue__pb2
asmicroservices.queue_pb2_grpc import queue__pb2