Dockerfile
from ubuntu:latest
RUN apt-get update -y && apt-get install -y python3-pip
COPY . /app
RUN pip3 install -r /app/requirements.txt
ENTRYPOINT ["python3"]
CMD ["app/app.py"]
Run Code Online (Sandbox Code Playgroud)
运行命令
docker build -t flaskapp .
docker run -it -d p 5000:5000 flaskapp
Run Code Online (Sandbox Code Playgroud)
如果我通过邮递员发送请求localhost:5000,我会得到一个Error: Socket hang up错误
有人知道为什么这是一个问题吗?
我目前正在遵循 grpc 中的一些快速入门教程,添加一些数据库连接,显然每个请求都创建一个数据库连接并不是最佳的
def connection():
conn = psycopg2.connect(
user="postgres", password="some_password", database="some_db")
return conn
class LeagueGameManager(start_pb2_grpc.GameManagerServicer):
async def CreateLGGame(self, request, context):
try:
conn = connection()
cursor = conn.cursor()
cursor.execute("some sql statement")
conn.commit()
cursor.close()
conn.close()
except OperationalError as e:
context.set_details(e)
context.set_code(grpc.StatusCode.INTERNAL)
cursor.close()
conn.close()
return
return start_pb2.GameReply(json_response=json.dumps(new_row[0]))
async def serve():
server = grpc.aio.server()
start_pb2_grpc.add_GameManagerServicer_to_server(
LeagueGameManager(), server)
listen_addr = '[::]:50051'
server.add_insecure_port(listen_addr)
logging.info("Starting server on %s", listen_addr)
await server.start()
await server.wait_for_termination()
Run Code Online (Sandbox Code Playgroud)
管理上述数据库连接的最佳方式是什么?