From 922a7791f3ab8c9e9825f45006d07094d846090e Mon Sep 17 00:00:00 2001 From: Fierelier Date: Tue, 2 May 2023 16:30:37 +0200 Subject: [PATCH] Move output_lock/output_buffer out of main --- .../pycapi/frontend/terminal_simple.py | 19 +++++++++++++++---- user/modules/pycapi/main.py | 10 ---------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/user/modules/pycapi/frontend/terminal_simple.py b/user/modules/pycapi/frontend/terminal_simple.py index 025b2e2..2a4f809 100644 --- a/user/modules/pycapi/frontend/terminal_simple.py +++ b/user/modules/pycapi/frontend/terminal_simple.py @@ -6,6 +6,7 @@ import readchar import getpass import textwrap import threading +import queue import platform if platform.system().startswith("Windows"): import win_unicode_console @@ -17,6 +18,16 @@ import app contentapi = app.loadmod("contentapi") config = app.mod["main"].config +# Output from the websocket can only get truly printed to the screen when this lock is released. +# We grab the lock when the user is in some kind of "input" mode, which blocks the websocket printing +# thread from processing the queue (if it has anything anyway) +output_lock = threading.Lock() +# Output from the websocket is ALWAYS buffered, and we use a thread-safe queue to add and remove +# output safely. We buffer all messages to ensure the order is preserved; if we SOMETIMES queued and +# SOMETIMES did not, we would need to be very careful about whether the queue was empty, which requires +# additional locking and etc. +output_buffer = queue.Queue() + MAXTITLE=25 MSGPREFIX=Back.GREEN + " " + Back.RESET + " " @@ -70,7 +81,7 @@ def ws_onopen(ws): # We get exclusive access to output while we're handling user input. This allows us to "pause" any output # the threaded output queue might want to do (we're more important) - with ws.output_lock: + with output_lock: if key == "h": print(" -- Help menu / Controls --") for key, value in commands.items(): @@ -117,8 +128,8 @@ def ws_onopen(ws): # Just a simple infinite loop which blocks on the queue until something is available def ws_print_loop(): while True: - next_output = ws.output_buffer.get() - with ws.output_lock: + next_output = output_buffer.get() + with output_lock: printr(next_output) # Set the main room; we want to wait until the websocket is open because this also sets your @@ -203,7 +214,7 @@ def get_message_history_string(ws): def ws_print(ws, output): # Queueing is supposed to be threadsafe, so just slap a new one in there. This will wake up # the printing thread automatically - ws.output_buffer.put(output) + output_buffer.put(output) # Set the room to listen to on the websocket. Will also update the userlist, if # it's appropriate to do so diff --git a/user/modules/pycapi/main.py b/user/modules/pycapi/main.py index 5bfb21c..4b29dcf 100644 --- a/user/modules/pycapi/main.py +++ b/user/modules/pycapi/main.py @@ -4,7 +4,6 @@ import logging import threading import toml import websocket -import queue from colorama import Fore, Back, Style, init as colorama_init, ansi as colorama_ansi import app @@ -74,15 +73,6 @@ def main(): ws.user_info = context.user_me() ws.main_config = config ws.ignored = {} # Just a tracking list for fun stats - # Output from the websocket can only get truly printed to the screen when this lock is released. - # We grab the lock when the user is in some kind of "input" mode, which blocks the websocket printing - # thread from processing the queue (if it has anything anyway) - ws.output_lock = threading.Lock() - # Output from the websocket is ALWAYS buffered, and we use a thread-safe queue to add and remove - # output safely. We buffer all messages to ensure the order is preserved; if we SOMETIMES queued and - # SOMETIMES did not, we would need to be very careful about whether the queue was empty, which requires - # additional locking and etc. - ws.output_buffer = queue.Queue() # Note that the current_room stuff is set on open if you've supplied a default room in config ws.current_room = 0