Files
crawler-plugin/app/blueprints/communication.py
铭坤 4df3945131 modified: app/ali_oss.py
new file:   app/amazon/del_brand.py
	new file:   app/amazon/main.py
	modified:   app/blueprints/__pycache__/brand.cpython-39.pyc
	modified:   app/blueprints/__pycache__/communication.cpython-39.pyc
	modified:   app/blueprints/__pycache__/main.cpython-39.pyc
	modified:   app/blueprints/brand.py
	new file:   "app/blueprints/brand_\345\244\207\344\273\275.py"
	modified:   app/blueprints/communication.py
	modified:   app/blueprints/main.py
	modified:   app/config.py
	modified:   app/main.py
2026-04-01 11:53:12 +08:00

83 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import threading
import time
from queue import Empty, Full
from flask import Blueprint, jsonify, request
from config import JSON_TASK_QUEUE,runing_task
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})
@communication_bp.route('/api/amazon/get_all_detail', methods=['GET', 'POST'])
def get_all_detail():
"""获取正在执行的删除品牌任务详情"""
request_id = _resolve_request_id()
if not request_id: #如果没有提供 id 参数,直接返回所有正在执行的任务详情
return jsonify({'success': True, 'data': runing_task})
task_info = runing_task.get(request_id)
if not task_info:
return jsonify({'success': False, 'message': '任务不存在', 'data': None})
return jsonify({'success': True, 'data': task_info})