Test threading lock

This commit is contained in:
Guy Van Sanden 2025-02-12 11:11:58 +01:00
parent 48791ee1f1
commit 38d9e87393

View File

@ -18,26 +18,25 @@ with open(r'/etc/tellme/hooks.yml') as hooksfile:
def sendmessage(target, message): def sendmessage(target, message):
print("Sendmessage %s called to %s" % (message, target)) print("Sendmessage %s called to %s" % (message, target))
with lock: msg = ("%s %s" % (target, message))
msg = ("%s %s" % (target, message))
# Create a unique correlation ID # Create a unique correlation ID
command = { command = {
"corrId": f"id{random.randint(0, 999999)}", "corrId": f"id{random.randint(0, 999999)}",
"cmd": msg, "cmd": msg,
} }
json_command = json.dumps(command) json_command = json.dumps(command)
""" Connects to WebSocket server, sends a message, and returns the response """ """ Connects to WebSocket server, sends a message, and returns the response """
uri = "ws://localhost:5080" uri = "ws://localhost:5080"
ws = websocket.create_connection(uri) # Blocking WebSocket connection ws = websocket.create_connection(uri) # Blocking WebSocket connection
ws.send(json_command) # Send message to WebSocket ws.send(json_command) # Send message to WebSocket
response = ws.recv() # Receive response response = ws.recv() # Receive response
print("Message %s sent over websocket to %s" % (message, target)) print("Message %s sent over websocket to %s" % (message, target))
# ws.close() # Close WebSocket connection # ws.close() # Close WebSocket connection
# print(response) # print(response)
ws.close() ws.close()
return response return response
@app.route("/webhook/<id>", methods=['POST']) @app.route("/webhook/<id>", methods=['POST'])
def webhook_receiver(id): def webhook_receiver(id):
@ -53,7 +52,8 @@ def webhook_receiver(id):
if target is not None: if target is not None:
print(target) print(target)
sendmessage(target, data.get('message')) with lock:
sendmessage(target, data.get('message'))
else: else:
print("No target found, dropping message") print("No target found, dropping message")