mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-28 03:05:25 +08:00
Merge pull request #150 from mnixry/patch-1
修复CommandSession的部分方法在多线程条件下出错
This commit is contained in:
commit
7eb49624c3
@ -3,6 +3,7 @@ import re
|
||||
import shlex
|
||||
import warnings
|
||||
from datetime import datetime
|
||||
from functools import partial
|
||||
from typing import (
|
||||
Tuple, Union, Callable, Iterable, Any, Optional, List, Dict,
|
||||
Awaitable
|
||||
@ -271,7 +272,7 @@ class CommandSession(BaseSession):
|
||||
__slots__ = ('cmd',
|
||||
'current_key', 'current_arg_filters', '_current_send_kwargs',
|
||||
'current_arg', '_current_arg_text', '_current_arg_images',
|
||||
'_state', '_last_interaction', '_running')
|
||||
'_state', '_last_interaction', '_running', '_run_future')
|
||||
|
||||
def __init__(self, bot: NoneBot, ctx: Context_T, cmd: Command, *,
|
||||
current_arg: str = '', args: Optional[CommandArgs_T] = None):
|
||||
@ -292,6 +293,8 @@ class CommandSession(BaseSession):
|
||||
self._current_arg_images = None
|
||||
self.refresh(ctx, current_arg=current_arg) # fill the above
|
||||
|
||||
self._run_future = partial(asyncio.run_coroutine_threadsafe, loop=bot.loop)
|
||||
|
||||
self._state: State_T = {}
|
||||
if args:
|
||||
self._state.update(args)
|
||||
@ -418,13 +421,13 @@ class CommandSession(BaseSession):
|
||||
def pause(self, message: Optional[Message_T] = None, **kwargs) -> None:
|
||||
"""Pause the session for further interaction."""
|
||||
if message:
|
||||
asyncio.ensure_future(self.send(message, **kwargs))
|
||||
self._run_future(self.send(message, **kwargs))
|
||||
raise _PauseException
|
||||
|
||||
def finish(self, message: Optional[Message_T] = None, **kwargs) -> None:
|
||||
"""Finish the session."""
|
||||
if message:
|
||||
asyncio.ensure_future(self.send(message, **kwargs))
|
||||
self._run_future(self.send(message, **kwargs))
|
||||
raise _FinishException
|
||||
|
||||
def switch(self, new_ctx_message: Message_T) -> None:
|
||||
|
Loading…
Reference in New Issue
Block a user