我目前正在尝试使用flask实现grpc(grpc作为服务器,flask作为客户端),但我一直面临这个问题:ModuleNotFoundError:当我尝试运行命令“flask run”时,没有名为“queue_pb2”的模块终端。
这是我当前的目录结构:
Cloud/
|-- app.py
|-- microservices/
|-- queue/
|-- protos/
|-- queue.proto
|-- queue_pb2.py
|-- queue_pb2_grpc.py
这是我在终端中使用的命令,用于在 microservices/queue 目录中生成 pb2 文件
python -m grpc_tools.protoc -I protos --python_out=. --pyi_out=. --grpc_python_out=. protos/queue.proto
这是我当前的 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()
这是我的队列.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);
}
这是queue_pb2_grpc.py 的一个片段,您可以看到行 importqueue_pb2 asqueue__pb2 在那里
# 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,
)
任何建议将不胜感激,因为我已经坚持这个问题很长一段时间了,谢谢!
将此导入声明
import queue_pb2 as queue__pb2
修改为 microservices.queue_pb2_grpc import queue__pb2
通过调用 D:\GitHub Repo\Cloud> python -m microservices.queue.queue_server 修复它