我正在使用Python
3.12.7
。
我有多个服务器,它们在网络环境中以多线程方式运行。
我想增加 self.msg_id
函数中的变量 handle_message
。
其他一切都运行良好。但是,当我执行 self.msg_id += 1
行时,我收到错误:
[ERROR] Attribute msg_id already defined. Plugin conflict?
从变量中读取值效果很好。 请注意,这是大学的一项任务,因此在代码中存在一些奇怪的注释。
Server类的init
class Server(Bottle):
def __init__(self, ID, IP, server_list):
super(Server, self).__init__()
self.id = int(ID)
self.ip = str(IP)
self.server_list = server_list
self.msg_id = 1
self.status = {
"crashed": False,
"notes": "",
"num_entries": 0, # we use this to generate ids for the entries
}
self.lock = threading.RLock() # use reentry lock for the server
self.msg_lock = threading.RLock()
# Handle CORS
self.route("/<:re:.*>", method="OPTIONS", callback=self.add_cors_headers)
self.add_hook("after_request", self.add_cors_headers)
# Those two http calls simulate crashes, i.e., unavailability of the server
self.post("/crash", callback=self.crash_request)
self.post("/recover", callback=self.recover_request)
self.get("/status", callback=self.status_request)
# Define REST URIs for the frontend (note that we define multiple update and delete routes right now)
self.post("/entries", callback=self.create_entry_request)
self.get("/entries", callback=self.list_entries_request)
self.put("/entries/<entry_id>", callback=self.update_entry_request)
self.delete("/entries/<entry_id>", callback=self.delete_entry_request)
# REST URIs for the later algorithms
self.post("/message", callback=self.message_request)
self.board = Board()
相关功能
def handle_message(self, message):
# Note that you might need to use the lock
print("Received message: ", message)
if "type" in message:
type = message["type"]
if type == "add_entry":
assert (
self.id == 0
) # ID 0 is coordinator only this server should receive add_entry messages right now
with self.msg_lock:
entry_value = message["entry_value"]
# We can safely propagate here since we always have a single frontend client, right? So no need to lock, right? Right?!
for other in self.server_list:
# TODO: Send message to other servers concurrently?
self.send_message(
other,
{
"type": "propagate",
"entry_value": entry_value,
"origin_id": self.id,
"message_id": self.msg_id,
},
)
self.msg_id += 1
elif type == "propagate":
entry_value = message["entry_value"]
msg_id = message["message_id"]
# Let's hope this is from the coordinator
with self.lock:
if msg_id in self.board.indexed_entries:
return {}
self.status["num_entries"] += 1
entry = Entry(msg_id, entry_value)
self.board.add_entry(entry)
else:
print("Received weird message?")
return {}
主要代码执行
# Sleep a bit to allow logging to be attached
time.sleep(2)
# the server_list contains all server ips of the distributed blackboard
server_list = os.getenv("SERVER_LIST").split(",")
own_id = int(os.getenv("SERVER_ID"))
own_ip = server_list[own_id]
server = Server(own_id, own_ip, server_list)
NUM_THREADS = 10
print("#### Starting Server {} with {} threads".format(str(own_id), NUM_THREADS))
httpserver.serve(
server,
host="0.0.0.0",
port=80,
threadpool_workers=NUM_THREADS,
threadpool_options={"spawn_if_under": NUM_THREADS},
)
我没有想法我没有想法 我没有想法
我没有想法 我没有想法