fix(platform): 防止绑定任务压垮连接池和事件循环
This commit is contained in:
@@ -7,12 +7,12 @@ from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import os
|
||||
import threading
|
||||
|
||||
from web.backend.database import WorkerSessionLocal
|
||||
|
||||
from .douyu_concurrency import clamp_douyu_task_concurrency
|
||||
from .douyu_runner import DouyuBatchRunner, douyu_batch_registry
|
||||
from .task_runtime import TaskLane, submit
|
||||
|
||||
|
||||
def _env_positive_int(name: str, default: int) -> int:
|
||||
@@ -30,6 +30,33 @@ DOUYU_BATCH_CREATE_CONCURRENCY = _env_positive_int(
|
||||
DOUYU_MAX_ACTIVE_BATCHES = _env_positive_int("DOUYU_MAX_ACTIVE_BATCHES", 10)
|
||||
|
||||
|
||||
def _run_batch(
|
||||
*,
|
||||
batch_id: str,
|
||||
task_type: str,
|
||||
payload: dict,
|
||||
concurrency: int,
|
||||
loop: asyncio.AbstractEventLoop | None,
|
||||
log_queue: asyncio.Queue | None,
|
||||
) -> None:
|
||||
"""Run one coordinator with a short-lived session owned by the worker."""
|
||||
thread_db = WorkerSessionLocal()
|
||||
try:
|
||||
runner = DouyuBatchRunner(
|
||||
db=thread_db,
|
||||
batch_id=batch_id,
|
||||
task_type=task_type,
|
||||
payload=payload,
|
||||
log_queue=log_queue,
|
||||
loop=loop,
|
||||
concurrency=clamp_douyu_task_concurrency(task_type, concurrency),
|
||||
)
|
||||
douyu_batch_registry.register(batch_id, log_queue, loop, runner)
|
||||
runner.run()
|
||||
finally:
|
||||
thread_db.close()
|
||||
|
||||
|
||||
def start_douyu_batch(
|
||||
*,
|
||||
batch_id: str,
|
||||
@@ -39,8 +66,14 @@ def start_douyu_batch(
|
||||
loop: asyncio.AbstractEventLoop | None = None,
|
||||
log_queue: asyncio.Queue | None = None,
|
||||
capture_event_loop: bool = True,
|
||||
) -> None:
|
||||
"""Create the batch coordinator; websocket logging is optional."""
|
||||
) -> bool:
|
||||
"""Queue a batch coordinator without creating a thread per order.
|
||||
|
||||
The coordinator uses the dedicated bounded lane and opens its database
|
||||
session only after a worker slot is available. Callers may therefore
|
||||
safely invoke this from a request thread without holding a database
|
||||
connection while waiting in the queue.
|
||||
"""
|
||||
if loop is None and capture_event_loop:
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
@@ -48,18 +81,17 @@ def start_douyu_batch(
|
||||
loop = None
|
||||
if log_queue is None and loop is not None:
|
||||
log_queue = asyncio.Queue()
|
||||
thread_db = WorkerSessionLocal()
|
||||
runner = DouyuBatchRunner(
|
||||
db=thread_db,
|
||||
future = submit(
|
||||
TaskLane.BATCH_COORDINATOR,
|
||||
_run_batch,
|
||||
batch_id=batch_id,
|
||||
task_type=task_type,
|
||||
payload=payload,
|
||||
log_queue=log_queue,
|
||||
concurrency=concurrency,
|
||||
loop=loop,
|
||||
concurrency=clamp_douyu_task_concurrency(task_type, concurrency),
|
||||
log_queue=log_queue,
|
||||
)
|
||||
douyu_batch_registry.register(batch_id, log_queue, loop, runner)
|
||||
threading.Thread(target=runner.run, daemon=True).start()
|
||||
return future is not None
|
||||
|
||||
|
||||
def stop_douyu_batches(batch_ids: list[str]) -> int:
|
||||
|
||||
Reference in New Issue
Block a user