Skip to content

Commit

Permalink
🐛 修复 loop 冲突的问题 (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
Well2333 authored Dec 18, 2023
1 parent 9fa0e29 commit fde4ddf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
24 changes: 18 additions & 6 deletions nonebot_plugin_bilichat/lib/uid_extract.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
import threading
from queue import Queue
from typing import Union

from nonebot.log import logger
Expand Down Expand Up @@ -40,11 +42,21 @@ async def uid_extract(text: str) -> Union[str, SearchUp]:


def uid_extract_sync(text: str) -> Union[str, SearchUp]:
try:
loop = asyncio.get_event_loop()
if loop.is_closed():
raise RuntimeError
except RuntimeError:
# 创建一个队列用于从线程中获取结果
queue = Queue()

# 定义一个运行异步函数并将结果放入队列的函数
def run_and_store_result():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(uid_extract(text))
result = loop.run_until_complete(uid_extract(text))
queue.put(result)
loop.close()

# 创建并启动线程
thread = threading.Thread(target=run_and_store_result)
thread.start()
thread.join()

# 从队列中获取返回结果
return queue.get()
7 changes: 1 addition & 6 deletions nonebot_plugin_bilichat/subscribe/manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import json
import time
from dataclasses import field
from typing import Any, Dict, List, Optional, Union

from nonebot import get_driver
Expand Down Expand Up @@ -48,10 +47,6 @@ def __init__(self, **values):
if isinstance(up, str):
raise ValueError(f"未找到 uid 为 {self.uid} 的 UP")
self.nickname = up.nickname
# 获取较大值的 living 与 dyn_offset
if up := SubscriptionSystem.uploaders.get(values["uid"]):
self.living = max(self.living, up.living)
self.dyn_offset = max(self.dyn_offset, up.dyn_offset)

@property
def subscribed_users(self) -> List["User"]:
Expand Down Expand Up @@ -291,7 +286,7 @@ def dict(cls):
def load(cls, data: Dict[str, Union[Dict[str, Any], List[Dict[str, Any]]]]):
raw_cfg = SubscriptionCfgFile.parse_obj(data)
cls.config = raw_cfg.config
cls.uploaders = {up.uid: up for up in raw_cfg.uploaders}
cls.uploaders.update({up.uid: up for up in raw_cfg.uploaders})
cls.users = {f"{u.platform}-_-{u.user_id}": u for u in raw_cfg.users}

# 清理无订阅的UP
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "nonebot-plugin-bilichat"

version = "5.0.0a4"
version = "5.0.0a5"

description = "多种B站链接解析,视频词云,AI总结,你想要的都在 bilichat"
authors = [
Expand Down

0 comments on commit fde4ddf

Please sign in to comment.