mirror of
https://github.com/nonebot/nonebot2.git
synced 2024-11-27 18:45:05 +08:00
✨ Feature: 将 block driver 转正为 none 驱动器 (#1522)
This commit is contained in:
parent
c73ca2b43f
commit
69edb98835
@ -21,7 +21,7 @@ from contextlib import asynccontextmanager
|
|||||||
from nonebot.typing import overrides
|
from nonebot.typing import overrides
|
||||||
from nonebot.drivers import Request, Response
|
from nonebot.drivers import Request, Response
|
||||||
from nonebot.exception import WebSocketClosed
|
from nonebot.exception import WebSocketClosed
|
||||||
from nonebot.drivers._block_driver import BlockDriver
|
from nonebot.drivers.none import Driver as NoneDriver
|
||||||
from nonebot.drivers import WebSocket as BaseWebSocket
|
from nonebot.drivers import WebSocket as BaseWebSocket
|
||||||
from nonebot.drivers import HTTPVersion, ForwardMixin, ForwardDriver, combine_driver
|
from nonebot.drivers import HTTPVersion, ForwardMixin, ForwardDriver, combine_driver
|
||||||
|
|
||||||
@ -170,5 +170,5 @@ class WebSocket(BaseWebSocket):
|
|||||||
await self.websocket.send_bytes(data)
|
await self.websocket.send_bytes(data)
|
||||||
|
|
||||||
|
|
||||||
Driver: Type[ForwardDriver] = combine_driver(BlockDriver, Mixin) # type: ignore
|
Driver: Type[ForwardDriver] = combine_driver(NoneDriver, Mixin) # type: ignore
|
||||||
"""AIOHTTP Driver"""
|
"""AIOHTTP Driver"""
|
||||||
|
@ -18,7 +18,7 @@ from typing import Type, AsyncGenerator
|
|||||||
from contextlib import asynccontextmanager
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
from nonebot.typing import overrides
|
from nonebot.typing import overrides
|
||||||
from nonebot.drivers._block_driver import BlockDriver
|
from nonebot.drivers.none import Driver as NoneDriver
|
||||||
from nonebot.drivers import (
|
from nonebot.drivers import (
|
||||||
Request,
|
Request,
|
||||||
Response,
|
Response,
|
||||||
@ -77,5 +77,5 @@ class Mixin(ForwardMixin):
|
|||||||
yield ws
|
yield ws
|
||||||
|
|
||||||
|
|
||||||
Driver: Type[ForwardDriver] = combine_driver(BlockDriver, Mixin) # type: ignore
|
Driver: Type[ForwardDriver] = combine_driver(NoneDriver, Mixin) # type: ignore
|
||||||
"""HTTPX Driver"""
|
"""HTTPX Driver"""
|
||||||
|
@ -1,12 +1,24 @@
|
|||||||
|
"""None 驱动适配
|
||||||
|
|
||||||
|
:::tip 提示
|
||||||
|
本驱动不支持任何服务器或客户端连接
|
||||||
|
:::
|
||||||
|
|
||||||
|
FrontMatter:
|
||||||
|
sidebar_position: 6
|
||||||
|
description: nonebot.drivers.none 模块
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
import signal
|
import signal
|
||||||
import asyncio
|
import asyncio
|
||||||
import threading
|
import threading
|
||||||
from typing import Set, Union, Callable, Awaitable, cast
|
from typing import Set, Union, Callable, Awaitable, cast
|
||||||
|
|
||||||
from nonebot.log import logger
|
from nonebot.log import logger
|
||||||
from nonebot.drivers import Driver
|
|
||||||
from nonebot.typing import overrides
|
from nonebot.typing import overrides
|
||||||
from nonebot.config import Env, Config
|
from nonebot.config import Env, Config
|
||||||
|
from nonebot.drivers import Driver as BaseDriver
|
||||||
from nonebot.utils import run_sync, is_coroutine_callable
|
from nonebot.utils import run_sync, is_coroutine_callable
|
||||||
|
|
||||||
HOOK_FUNC = Union[Callable[[], None], Callable[[], Awaitable[None]]]
|
HOOK_FUNC = Union[Callable[[], None], Callable[[], Awaitable[None]]]
|
||||||
@ -16,7 +28,9 @@ HANDLED_SIGNALS = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class BlockDriver(Driver):
|
class Driver(BaseDriver):
|
||||||
|
"""None 驱动框架"""
|
||||||
|
|
||||||
def __init__(self, env: Env, config: Config):
|
def __init__(self, env: Env, config: Config):
|
||||||
super().__init__(env, config)
|
super().__init__(env, config)
|
||||||
self.startup_funcs: Set[HOOK_FUNC] = set()
|
self.startup_funcs: Set[HOOK_FUNC] = set()
|
||||||
@ -25,18 +39,18 @@ class BlockDriver(Driver):
|
|||||||
self.force_exit: bool = False
|
self.force_exit: bool = False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@overrides(Driver)
|
@overrides(BaseDriver)
|
||||||
def type(self) -> str:
|
def type(self) -> str:
|
||||||
"""驱动名称: `block_driver`"""
|
"""驱动名称: `none`"""
|
||||||
return "block_driver"
|
return "none"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@overrides(Driver)
|
@overrides(BaseDriver)
|
||||||
def logger(self):
|
def logger(self):
|
||||||
"""block driver 使用的 logger"""
|
"""none driver 使用的 logger"""
|
||||||
return logger
|
return logger
|
||||||
|
|
||||||
@overrides(Driver)
|
@overrides(BaseDriver)
|
||||||
def on_startup(self, func: HOOK_FUNC) -> HOOK_FUNC:
|
def on_startup(self, func: HOOK_FUNC) -> HOOK_FUNC:
|
||||||
"""
|
"""
|
||||||
注册一个启动时执行的函数
|
注册一个启动时执行的函数
|
||||||
@ -44,7 +58,7 @@ class BlockDriver(Driver):
|
|||||||
self.startup_funcs.add(func)
|
self.startup_funcs.add(func)
|
||||||
return func
|
return func
|
||||||
|
|
||||||
@overrides(Driver)
|
@overrides(BaseDriver)
|
||||||
def on_shutdown(self, func: HOOK_FUNC) -> HOOK_FUNC:
|
def on_shutdown(self, func: HOOK_FUNC) -> HOOK_FUNC:
|
||||||
"""
|
"""
|
||||||
注册一个停止时执行的函数
|
注册一个停止时执行的函数
|
||||||
@ -52,22 +66,22 @@ class BlockDriver(Driver):
|
|||||||
self.shutdown_funcs.add(func)
|
self.shutdown_funcs.add(func)
|
||||||
return func
|
return func
|
||||||
|
|
||||||
@overrides(Driver)
|
@overrides(BaseDriver)
|
||||||
def run(self, *args, **kwargs):
|
def run(self, *args, **kwargs):
|
||||||
"""启动 block driver"""
|
"""启动 none driver"""
|
||||||
super().run(*args, **kwargs)
|
super().run(*args, **kwargs)
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.run_until_complete(self.serve())
|
loop.run_until_complete(self._serve())
|
||||||
|
|
||||||
async def serve(self):
|
async def _serve(self):
|
||||||
self.install_signal_handlers()
|
self._install_signal_handlers()
|
||||||
await self.startup()
|
await self._startup()
|
||||||
if self.should_exit.is_set():
|
if self.should_exit.is_set():
|
||||||
return
|
return
|
||||||
await self.main_loop()
|
await self._main_loop()
|
||||||
await self.shutdown()
|
await self._shutdown()
|
||||||
|
|
||||||
async def startup(self):
|
async def _startup(self):
|
||||||
# run startup
|
# run startup
|
||||||
cors = [
|
cors = [
|
||||||
cast(Callable[..., Awaitable[None]], startup)()
|
cast(Callable[..., Awaitable[None]], startup)()
|
||||||
@ -86,10 +100,10 @@ class BlockDriver(Driver):
|
|||||||
|
|
||||||
logger.info("Application startup completed.")
|
logger.info("Application startup completed.")
|
||||||
|
|
||||||
async def main_loop(self):
|
async def _main_loop(self):
|
||||||
await self.should_exit.wait()
|
await self.should_exit.wait()
|
||||||
|
|
||||||
async def shutdown(self):
|
async def _shutdown(self):
|
||||||
logger.info("Shutting down")
|
logger.info("Shutting down")
|
||||||
|
|
||||||
logger.info("Waiting for application shutdown.")
|
logger.info("Waiting for application shutdown.")
|
||||||
@ -130,7 +144,7 @@ class BlockDriver(Driver):
|
|||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
loop.stop()
|
loop.stop()
|
||||||
|
|
||||||
def install_signal_handlers(self) -> None:
|
def _install_signal_handlers(self) -> None:
|
||||||
if threading.current_thread() is not threading.main_thread():
|
if threading.current_thread() is not threading.main_thread():
|
||||||
# Signals can only be listened to from the main thread.
|
# Signals can only be listened to from the main thread.
|
||||||
return
|
return
|
||||||
@ -139,13 +153,13 @@ class BlockDriver(Driver):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
for sig in HANDLED_SIGNALS:
|
for sig in HANDLED_SIGNALS:
|
||||||
loop.add_signal_handler(sig, self.handle_exit, sig, None)
|
loop.add_signal_handler(sig, self._handle_exit, sig, None)
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
# Windows
|
# Windows
|
||||||
for sig in HANDLED_SIGNALS:
|
for sig in HANDLED_SIGNALS:
|
||||||
signal.signal(sig, self.handle_exit)
|
signal.signal(sig, self._handle_exit)
|
||||||
|
|
||||||
def handle_exit(self, sig, frame):
|
def _handle_exit(self, sig, frame):
|
||||||
if self.should_exit.is_set():
|
if self.should_exit.is_set():
|
||||||
self.force_exit = True
|
self.force_exit = True
|
||||||
else:
|
else:
|
@ -23,7 +23,7 @@ from nonebot.typing import overrides
|
|||||||
from nonebot.log import LoguruHandler
|
from nonebot.log import LoguruHandler
|
||||||
from nonebot.drivers import Request, Response
|
from nonebot.drivers import Request, Response
|
||||||
from nonebot.exception import WebSocketClosed
|
from nonebot.exception import WebSocketClosed
|
||||||
from nonebot.drivers._block_driver import BlockDriver
|
from nonebot.drivers.none import Driver as NoneDriver
|
||||||
from nonebot.drivers import WebSocket as BaseWebSocket
|
from nonebot.drivers import WebSocket as BaseWebSocket
|
||||||
from nonebot.drivers import ForwardMixin, ForwardDriver, combine_driver
|
from nonebot.drivers import ForwardMixin, ForwardDriver, combine_driver
|
||||||
|
|
||||||
@ -128,5 +128,5 @@ class WebSocket(BaseWebSocket):
|
|||||||
await self.websocket.send(data)
|
await self.websocket.send(data)
|
||||||
|
|
||||||
|
|
||||||
Driver: Type[ForwardDriver] = combine_driver(BlockDriver, Mixin) # type: ignore
|
Driver: Type[ForwardDriver] = combine_driver(NoneDriver, Mixin) # type: ignore
|
||||||
"""Websockets Driver"""
|
"""Websockets Driver"""
|
||||||
|
@ -149,7 +149,7 @@ async def test_http_driver(app: App):
|
|||||||
),
|
),
|
||||||
pytest.param(
|
pytest.param(
|
||||||
{"driver": "~httpx:Driver+~websockets"},
|
{"driver": "~httpx:Driver+~websockets"},
|
||||||
"block_driver+httpx+websockets",
|
"none+httpx+websockets",
|
||||||
id="httpx+websockets",
|
id="httpx+websockets",
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
@ -48,5 +48,15 @@
|
|||||||
"homepage": "/docs/tutorial/choose-driver",
|
"homepage": "/docs/tutorial/choose-driver",
|
||||||
"tags": [],
|
"tags": [],
|
||||||
"is_official": true
|
"is_official": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"module_name": "nonebot.drivers.none",
|
||||||
|
"project_link": "",
|
||||||
|
"name": "None",
|
||||||
|
"desc": "None 驱动器",
|
||||||
|
"author": "yanyongyu",
|
||||||
|
"homepage": "/docs/tutorial/choose-driver",
|
||||||
|
"tags": [],
|
||||||
|
"is_official": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user