处理可能出现的锁问题

This commit is contained in:
zk
2026-06-02 19:20:38 +08:00
parent b4498813ab
commit 85278ea96a
+27 -20
View File
@@ -228,35 +228,42 @@ async def _extract_skill_tags(job_id: int, result: dict) -> None:
now = datetime.now() now = datetime.now()
tag_ids = [] tag_ids = []
async with MysqlSession() as mysql: for name in skills:
for name in skills: name = str(name).strip().lower()
name = str(name).strip().lower() if not name or len(name) > 50:
if not name or len(name) > 50: continue
continue
tag_id = next(_id_gen) # 每个 tag 单独 session,避免死锁
# INSERT IGNORE real_id = await _find_or_create_skill_tag(name)
await mysql.execute( if real_id and real_id not in tag_ids:
text("INSERT IGNORE INTO bg_skill_tag (id, name) VALUES (:id, :name)"), tag_ids.append(real_id)
{"id": tag_id, "name": name},
)
# 查回真实ID
row = await mysql.execute(
text("SELECT id FROM bg_skill_tag WHERE name = :name LIMIT 1"),
{"name": name},
)
real_id = row.scalar()
if real_id and real_id not in tag_ids:
tag_ids.append(real_id)
if tag_ids: if tag_ids:
async with MysqlSession() as mysql:
await mysql.execute( await mysql.execute(
insert(JobSkillTagRelation), insert(JobSkillTagRelation),
[{"id": next(_id_gen), "job_id": job_id, "skill_tag_id": tid, "create_time": now} for tid in tag_ids], [{"id": next(_id_gen), "job_id": job_id, "skill_tag_id": tid, "create_time": now} for tid in tag_ids],
) )
await mysql.commit()
async def _find_or_create_skill_tag(name: str) -> int | None:
"""查找或创建技能标签(单独事务,避免死锁)"""
async with MysqlSession() as mysql:
tag_id = next(_id_gen)
await mysql.execute(
text("INSERT IGNORE INTO bg_skill_tag (id, name) VALUES (:id, :name)"),
{"id": tag_id, "name": name},
)
await mysql.commit() await mysql.commit()
async with MysqlSession() as mysql:
row = await mysql.execute(
text("SELECT id FROM bg_skill_tag WHERE name = :name LIMIT 1"),
{"name": name},
)
return row.scalar()
async def _find_or_create_company(short_name: str) -> int: async def _find_or_create_company(short_name: str) -> int:
"""查找或创建公司(加锁防并发重复)""" """查找或创建公司(加锁防并发重复)"""