Skip to content

Commit fc4f166

Browse files
committed
may fix #49
1 parent 0d46681 commit fc4f166

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ Telegram:[@lgc2333](https://t.me/lgc2333)
174174

175175
## 📝 更新日志
176176

177+
### 2.1.2
178+
179+
- fix [#49](https://github.com/lgc-NB2Dev/nonebot-plugin-picstatus/issues/49)
180+
177181
### 2.1.1
178182

179183
- 重新加入指令带图/回复图自定义背景功能

nonebot_plugin_picstatus/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ async def _():
5050
if config.ps_only_su:
5151
usage += "\n注意:仅SuperUser可以使用此指令"
5252

53-
__version__ = "2.1.1"
53+
__version__ = "2.1.2"
5454
__plugin_meta__ = PluginMetadata(
5555
name="PicStatus",
5656
description="以图片形式显示当前设备的运行状态",

nonebot_plugin_picstatus/bg_provider.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,21 @@ def __init__(self, preload_count: int):
135135
self.preload_count = preload_count
136136
self.backgrounds: List[BgData] = []
137137
self.tasks: List[aio.Task[None]] = []
138-
self.task_signal = aio.Future[None]()
138+
self.task_signal: Optional[aio.Future[None]] = None
139139
self.signal_wait_lock = aio.Lock()
140140

141+
def _get_signal(self) -> aio.Future[None]:
142+
if (not self.task_signal) or self.task_signal.done():
143+
self.task_signal = aio.Future()
144+
return self.task_signal
145+
146+
def _wait_signal(self):
147+
async def inner():
148+
async with self.signal_wait_lock:
149+
await self._get_signal()
150+
151+
return aio.wait_for(inner(), timeout=15)
152+
141153
def create_preload_task(self):
142154
async def task_func():
143155
logger.debug("Started a preload background task")
@@ -148,13 +160,13 @@ async def task_func():
148160
# if error occurred this should be an unexpected error
149161
# need to let this error raise
150162
logger.opt(exception=e).debug("Exception when preloading")
151-
if not self.task_signal.done():
152-
self.task_signal.set_exception(e)
163+
if not (s := self._get_signal()).done():
164+
s.set_exception(e)
153165
else:
154166
logger.debug("A preload task done")
155167
self.backgrounds.append(bg)
156-
if not self.task_signal.done():
157-
self.task_signal.set_result(None)
168+
if not (s := self._get_signal()).done():
169+
s.set_result(None)
158170
finally:
159171
self.tasks.remove(task)
160172

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

174-
async def wait_signal(self):
175-
async def inner():
176-
async with self.signal_wait_lock:
177-
if self.task_signal.done():
178-
self.task_signal = aio.Future()
179-
await self.task_signal
180-
181-
return await aio.wait_for(inner(), timeout=15)
182-
183186
async def get(self) -> BgData:
184187
if not self.backgrounds:
185188
self.start_preload(create_when_full=True)
186189
if self.tasks:
187-
await self.wait_signal()
190+
await self._wait_signal()
188191
if not self.backgrounds:
189192
raise RuntimeError("Failed to wait background")
190193
bg = self.backgrounds.pop(0)

0 commit comments

Comments
 (0)