From ea2141dc6fd7284e6f9fa04ee638460286e3b09c Mon Sep 17 00:00:00 2001 From: Christopher Baklid Date: Sat, 26 May 2018 12:54:49 +0200 Subject: simplify threads and use local thread variables to manage user websocket connections --- snekbox.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'snekbox.py') diff --git a/snekbox.py b/snekbox.py index ee82be8..d6795e8 100644 --- a/snekbox.py +++ b/snekbox.py @@ -1,13 +1,12 @@ import sys import io import json +import logging -from rmq.consumer import consume -from rmq.publisher import publish +from logs import log +from rmq import Rmq -from config import HOST -from config import EXCHANGE_TYPE -from config import QUEUE +rmq = Rmq() def execute(snippet): @@ -26,24 +25,21 @@ def execute(snippet): return redirected_output.getvalue().strip() -def message_handler(ch, method, properties, body): +def message_handler(ch, method, properties, body, thread_ws=None): msg = body.decode('utf-8') - print(f"incoming: {msg}", flush=True) + log.info(f"incoming: {msg}") snek_msg = json.loads(msg) for snekid, snekcode in snek_msg.items(): result = execute(snekcode) - print(f"outgoing: {result}", flush=True) - publish(result, - host=HOST, - queue=snekid, - routingkey=snekid, - exchange=snekid, - exchange_type=EXCHANGE_TYPE) - + log.info(f"outgoing: {result}") + rmq.publish(result, + queue=snekid, + routingkey=snekid, + exchange=snekid) ch.basic_ack(delivery_tag=method.delivery_tag) if __name__ == '__main__': - consume(host=HOST, queue=QUEUE, callback=message_handler) + rmq.consume(callback=message_handler) -- cgit v1.2.3