大佬教程收集整理的这篇文章主要介绍了使用 multiprocessing.manangers.BaseManager 作为 docker-compose 编排的一部分,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我过去经常使用 docker-compose,并且利用作为 docker-compose 编排一部分创建的内部网络取得了成功。
我有一个非常简单的例子,我有两个服务:
version: '3'
services:
intent:
restart: always
build: ./dockerfs/intent
command: gunicorn -w 2 --bind 0.0.0.0:5000 --timeout 999 --log-level deBUG clIEnt:app
ports:
- 8075:5000
base_s:
restart: always
build: ./dockerfs/base
其中服务 base_s
是一个 base_s 服务,它通过连接到在 connections
中“保存”全局数据的 BaseManager 服务器:
import Json
import uuID
from multiprocessing import Lock
from multiprocessing.managers import BaseManager
connections = {}
lock = Lock()
def get_connection(intent_ID):
with lock:
if intent_ID not in connections:
print(f"{intent_ID} not in connections. creaTing Now...")
connections[intent_ID] = object()
print(f"addining intent_ID {intent_ID} to connections")
return connections[intent_ID]
print("starTing BaseManager!")
manager = BaseManager(("localhost",8098),b"password")
manager.register("get_connection",get_connection)
server = manager.get_server()
server.serve_forever()
服务意图是一个从 BaseManager 服务“获取”数据的 Flask 应用程序:
import uuID
from multiprocessing.managers import BaseManager
from flask import g,session,Flask,Jsonify
app = Flask(__name__)
clIEnt_ID = uuID.uuID4().hex
def get_clIEnt():
# if not hasattr(g,"rserve"):
# calling http:<service> as I normally would to "interact" with service in shared network
manager = BaseManager(("http://base_s",b"password") # <<
manager.register("get_connection")
manager.connect()
o = manager.get_connection(clIEnt_ID)
print(f"got object using ID {o}")
return f"got it {o}"
@app.route("/",methods=["GET","POST"])
def clIEnt():
o = get_clIEnt()
return Jsonify({"clIEnt_ID": clIEnt_ID,"object_id": o}
我可以在本地主机上成功运行上述代码,但是当我使用上面共享的 docker-compose
文件启动这些服务时,我得到一个 intent_1 | socket.gaIError: [Errno -2] name or service not kNown
。
我的印象是我可以与 BaseManager 服务交互,因为 docker-compose 维护着一个内部网络,这让我觉得我可能不了解 BaseManager 是如何提供服务的。
谁能想到为什么我无法连接到 BaseManager 服务?
提前致谢。
更新:
作为 netstat -a
服务中 base_s
命令的一部分,我看到
ActivE internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 ip-127-0-0-11.ec2:37121 0.0.0.0:* ListEN
tcp 0 0 0.0.0.0:8098 0.0.0.0:* ListEN
udp 0 0 ip-127-0-0-11.ec2:53041 0.0.0.0:*
所以看起来一切都在倾听。
成功了。
我所做的两个更改是:
在 base_s
服务代码中
这会选择一个默认地址,以确保您没有监听环回地址
在客户端flask api代码中 2) @H_920_7@manager = BaseManager(("http://base_s",b"password") 到
我认为真正的解决方法是 2
- 我认为 BaseManager 不需要 http 连接。
以上是大佬教程为你收集整理的使用 multiprocessing.manangers.BaseManager 作为 docker-compose 编排的一部分全部内容,希望文章能够帮你解决使用 multiprocessing.manangers.BaseManager 作为 docker-compose 编排的一部分所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。