feat: 添加线程锁

This commit is contained in:
caoqianming 2025-09-10 16:08:51 +08:00
parent be409273bb
commit 78da5f3873
1 changed files with 43 additions and 32 deletions

View File

@ -20,7 +20,8 @@ from pystray import MenuItem
import socket import socket
import logging import logging
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
import sys import time
import threading
CUR_DIR = os.path.dirname(os.path.abspath(__file__)) CUR_DIR = os.path.dirname(os.path.abspath(__file__))
@ -142,11 +143,19 @@ def save_pdf_from_binary(pdf_binary, file_path):
with open(file_path, 'wb') as file: with open(file_path, 'wb') as file:
file.write(pdf_binary) file.write(pdf_binary)
request_lock = threading.Lock()
@app.route('/prints/', methods=['POST', 'OPTIONS']) @app.route('/prints/', methods=['POST', 'OPTIONS'])
def str_printer(): def str_printer():
if request.method == 'OPTIONS': # 跨域请求预检 if request.method == 'OPTIONS': # 跨域请求预检
return jsonify({"message": "OK"}), 200 return jsonify({"message": "OK"}), 200
if not request_lock.acquire(timeout=30):
logger.warning("打印请求超时,系统繁忙")
return jsonify({
"err_msg": "系统繁忙,请稍后再试",
"err_code": "system_busy"
}), 503
try:
print_commands = request.json.get('printer_commands', None) print_commands = request.json.get('printer_commands', None)
print_name = request.json.get('printer_name', None) print_name = request.json.get('printer_name', None)
tsclibrary = ctypes.WinDLL(".//TSCLIB.dll") tsclibrary = ctypes.WinDLL(".//TSCLIB.dll")
@ -179,6 +188,8 @@ def str_printer():
return jsonify({"err_msg": "打印失败", "err_code": "print_failed"}), 500 return jsonify({"err_msg": "打印失败", "err_code": "print_failed"}), 500
return jsonify({}), 200 return jsonify({}), 200
finally:
request_lock.release()
if __name__ == '__main__': if __name__ == '__main__':