-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.py
More file actions
51 lines (43 loc) · 1.99 KB
/
Copy pathworker.py
File metadata and controls
51 lines (43 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import asyncio
from typing import Any
from sanic import Sanic
from enhancers import get_enhancer
from parsers import ChunkData, ChunkType, get_parser, load_all_parsers
async def worker(app: Sanic) -> dict[str, Any]:
# 使用工厂获取合适的解析器
load_all_parsers()
redis = app.ctx.redis
while True:
task = await redis.get_task()
if not task:
await asyncio.sleep(1)
continue
file_path = task.get("file_path")
parser = get_parser(file_path)
if not parser:
continue
parse_result = await parser.parse(file_path)
if not parse_result.success:
continue
# 控制并发数量,防止访问量过大导致失败
SEMAPHORE_LIMIT = 10
semaphore = asyncio.Semaphore(SEMAPHORE_LIMIT)
async def enhance_with_semaphore(chunk: ChunkData, semaphore: asyncio.Semaphore) -> ChunkData:
async with semaphore:
enhancer = get_enhancer(ChunkType(chunk.type))
if not enhancer:
return chunk
return await enhancer.enhance(chunk)
text_tasks = [enhance_with_semaphore(chunk, semaphore) for chunk in parse_result.texts]
table_tasks = [enhance_with_semaphore(chunk, semaphore) for chunk in parse_result.tables]
image_tasks = [enhance_with_semaphore(chunk, semaphore) for chunk in parse_result.images]
formula_tasks = [enhance_with_semaphore(chunk, semaphore) for chunk in parse_result.formulas]
text_chunk_list = await asyncio.gather(*text_tasks)
table_chunk_list = await asyncio.gather(*table_tasks)
image_chunk_list = await asyncio.gather(*image_tasks)
formula_chunk_list = await asyncio.gather(*formula_tasks)
parse_result.texts = text_chunk_list
parse_result.tables = table_chunk_list
parse_result.images = image_chunk_list
parse_result.formulas = formula_chunk_list
return parse_result.model_dump(mode="json")