Skip to content

Commit

Permalink
may fix #49
Browse files Browse the repository at this point in the history
  • Loading branch information
lgc2333 committed Aug 28, 2024
1 parent 0d46681 commit fc4f166
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ Telegram:[@lgc2333](https://t.me/lgc2333)

## 📝 更新日志

### 2.1.2

- fix [#49](https://github.com/lgc-NB2Dev/nonebot-plugin-picstatus/issues/49)

### 2.1.1

- 重新加入指令带图/回复图自定义背景功能
Expand Down
2 changes: 1 addition & 1 deletion nonebot_plugin_picstatus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def _():
if config.ps_only_su:
usage += "\n注意:仅SuperUser可以使用此指令"

__version__ = "2.1.1"
__version__ = "2.1.2"
__plugin_meta__ = PluginMetadata(
name="PicStatus",
description="以图片形式显示当前设备的运行状态",
Expand Down
33 changes: 18 additions & 15 deletions nonebot_plugin_picstatus/bg_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,21 @@ def __init__(self, preload_count: int):
self.preload_count = preload_count
self.backgrounds: List[BgData] = []
self.tasks: List[aio.Task[None]] = []
self.task_signal = aio.Future[None]()
self.task_signal: Optional[aio.Future[None]] = None
self.signal_wait_lock = aio.Lock()

def _get_signal(self) -> aio.Future[None]:
if (not self.task_signal) or self.task_signal.done():
self.task_signal = aio.Future()
return self.task_signal

def _wait_signal(self):
async def inner():
async with self.signal_wait_lock:
await self._get_signal()

return aio.wait_for(inner(), timeout=15)

def create_preload_task(self):
async def task_func():
logger.debug("Started a preload background task")
Expand All @@ -148,13 +160,13 @@ async def task_func():
# if error occurred this should be an unexpected error
# need to let this error raise
logger.opt(exception=e).debug("Exception when preloading")
if not self.task_signal.done():
self.task_signal.set_exception(e)
if not (s := self._get_signal()).done():
s.set_exception(e)
else:
logger.debug("A preload task done")
self.backgrounds.append(bg)
if not self.task_signal.done():
self.task_signal.set_result(None)
if not (s := self._get_signal()).done():
s.set_result(None)
finally:
self.tasks.remove(task)

Expand All @@ -171,20 +183,11 @@ def start_preload(self, create_when_full: bool = False):
for _ in range(task_count):
self.create_preload_task()

async def wait_signal(self):
async def inner():
async with self.signal_wait_lock:
if self.task_signal.done():
self.task_signal = aio.Future()
await self.task_signal

return await aio.wait_for(inner(), timeout=15)

async def get(self) -> BgData:
if not self.backgrounds:
self.start_preload(create_when_full=True)
if self.tasks:
await self.wait_signal()
await self._wait_signal()
if not self.backgrounds:
raise RuntimeError("Failed to wait background")
bg = self.backgrounds.pop(0)
Expand Down

0 comments on commit fc4f166

Please sign in to comment.