modified: app/ali_oss.py modified: app/app.py new file: app/blueprints/__pycache__/communication.cpython-39.pyc modified: app/blueprints/communication.py modified: app/brand_spider/__pycache__/main.cpython-39.pyc modified: app/config.py modified: app/main.py modified: app/static/bg.jpg modified: app/tool/__pycache__/devices.cpython-39.pyc modified: app/web_source/brand.html modified: app/web_source/templates_backup/brand.html
68 lines
2.6 KiB
Python
68 lines
2.6 KiB
Python
import threading
|
||
import time
|
||
from queue import Empty, Full
|
||
|
||
from flask import Blueprint, jsonify, request
|
||
|
||
from config import JSON_TASK_QUEUE
|
||
|
||
|
||
communication_bp = Blueprint('communication', __name__)
|
||
|
||
_del_brand_cache = {}
|
||
_del_brand_cache_lock = threading.Lock()
|
||
_latest_del_brand_item = None
|
||
_latest_del_brand_ts = 0.0
|
||
|
||
|
||
def _resolve_request_id():
|
||
rid = request.args.get('id')
|
||
if rid is not None and str(rid).strip() != '':
|
||
return str(rid)
|
||
if request.method == 'POST':
|
||
body = request.get_json(silent=True) or {}
|
||
rid = body.get('id')
|
||
if rid is not None and str(rid).strip() != '':
|
||
return str(rid)
|
||
return None
|
||
|
||
|
||
@communication_bp.route('/api/amazon/del_brand', methods=['GET', 'POST'])
|
||
def del_brand():
|
||
"""返回队列中的一个元素;同一 id 重复请求返回已缓存的同一元素;2分钟内不同 id 也返回最近元素。"""
|
||
global _latest_del_brand_item, _latest_del_brand_ts
|
||
request_id = _resolve_request_id()
|
||
if not request_id:
|
||
return jsonify({'success': False, 'message': '缺少参数 id', 'data': None}), 400
|
||
|
||
with _del_brand_cache_lock:
|
||
if request_id in _del_brand_cache:
|
||
item = _del_brand_cache[request_id]
|
||
print("【删除品牌】缓存命中", request_id, item)
|
||
return jsonify({'success': True, 'data': item})
|
||
if _latest_del_brand_item is not None and (time.time() - _latest_del_brand_ts) <= 120:
|
||
_del_brand_cache[request_id] = _latest_del_brand_item
|
||
print("【删除品牌】2分钟窗口命中", request_id, _latest_del_brand_item)
|
||
return jsonify({'success': True, 'data': _latest_del_brand_item})
|
||
|
||
try:
|
||
item = JSON_TASK_QUEUE.get_nowait()
|
||
except Empty:
|
||
print("队列为空")
|
||
return jsonify({'success': False, 'message': '队列为空', 'data': None})
|
||
|
||
with _del_brand_cache_lock:
|
||
if request_id in _del_brand_cache:
|
||
try:
|
||
JSON_TASK_QUEUE.put_nowait(item)
|
||
except Full:
|
||
print("【删除品牌】队列已满,无法归还重复拉取的任务")
|
||
item = _del_brand_cache[request_id]
|
||
print("【删除品牌】并发归并", request_id, item)
|
||
return jsonify({'success': True, 'data': item})
|
||
_del_brand_cache[request_id] = item
|
||
_latest_del_brand_item = item
|
||
_latest_del_brand_ts = time.time()
|
||
print("【删除品牌】返回任务数据", item)
|
||
return jsonify({'success': True, 'data': item})
|